Command line input in Python
It is not at all clear what the OP meant (even after some back-and-forth in the comments), but here are two answers to possible interpretations of the question:
For interactive user input (or piped commands or redirected input)
Use raw_input
in Python 2.x, and input
in Python 3. (These are built in, so you don't need to import anything to use them; you just have to use the right one for your version of python.)
For example:
user_input = raw_input("Some input please: ")
More details can be found here.
So, for example, you might have a script that looks like this
# First, do some work, to show -- as requested -- that# the user input doesn't need to come first.from __future__ import print_functionvar1 = 'tok'var2 = 'tik'+var1print(var1, var2)# Now ask for inputuser_input = raw_input("Some input please: ") # or `input("Some...` in python 3# Now do something with the aboveprint(user_input)
If you saved this in foo.py
, you could just call the script from the command line, it would print out tok tiktok
, then ask you for input. You could enter bar baz
(followed by the enter key) and it would print bar baz
. Here's what that would look like:
$ python foo.pytok tiktokSome input please: bar bazbar baz
Here, $
represents the command-line prompt (so you don't actually type that), and I hit Enter
after typing bar baz
when it asked for input.
For command-line arguments
Suppose you have a script named foo.py
and want to call it with arguments bar
and baz
from the command line like
$ foo.py bar baz
(Again, $
represents the command-line prompt.) Then, you can do that with the following in your script:
import sysarg1 = sys.argv[1]arg2 = sys.argv[2]
Here, the variable arg1
will contain the string 'bar'
, and arg2
will contain 'baz'
. The object sys.argv
is just a list containing everything from the command line. Note that sys.argv[0]
is the name of the script. And if, for example, you just want a single list of all the arguments, you would use sys.argv[1:]
.
Just Taking Input
the_input = raw_input("Enter input: ")
And that's it.
Moreover, if you want to make a list of inputs, you can do something like:
a = []for x in xrange(1,10): a.append(raw_input("Enter Data: "))
In that case, you'll be asked for data 10 times to store 9 items in a list.
Output:
Enter data: 2Enter data: 3Enter data: 4Enter data: 5Enter data: 7Enter data: 3Enter data: 8Enter data: 22Enter data: 5>>> a['2', '3', '4', '5', '7', '3', '8', '22', '5']
You can search that list the fundamental way with something like (after making that list):
if '2' in a: print "Found"
else: print "Not found."
You can replace '2' with "raw_input()" like this:
if raw_input("Search for: ") in a: print "Found"else: print "Not found"
Taking Raw Data From Input File via Commandline Interface
If you want to take the input from a file you feed through commandline (which is normally what you need when doing code problems for competitions, like Google Code Jam or the ACM/IBM ICPC):
example.py
while(True): line = raw_input() print "input data: %s" % line
In command line interface:
example.py < input.txt
Hope that helps.
If you're using Python 3, raw_input
has changed to input
Python 3 example:
line = input('Enter a sentence:')