Command line input in Python

172,314

Solution 1

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_function
var1 = 'tok'
var2 = 'tik'+var1
print(var1, var2)

# Now ask for input
user_input = raw_input("Some input please: ") # or `input("Some...` in python 3

# Now do something with the above
print(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.py
tok tiktok
Some input please: bar baz
bar 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 sys
arg1 = 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:].

Solution 2

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: 2
Enter data: 3
Enter data: 4
Enter data: 5
Enter data: 7
Enter data: 3
Enter data: 8
Enter data: 22
Enter 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.

Solution 3

If you're using Python 3, raw_input has changed to input

Python 3 example:

line = input('Enter a sentence:')

Solution 4

Start your script with the following line. The script will first run and then you will get the python command prompt. At this point all variables and functions will be available for interactive use and invocations.

#!/usr/bin/env python -i

Share:
172,314
Viin
Author by

Viin

Updated on July 29, 2022

Comments

  • Viin
    Viin almost 2 years

    Is it possible to run first the program then wait for the input of the user in command line. e.g.

    Run...
    
    Process...
    
    Input from the user(in command line form)...
    
    Process...
    
  • Viin
    Viin about 11 years
    thank you, but i need command line param rather than a user prompt. is that possible?
  • Mike
    Mike about 11 years
    Oh, yes. I'll change my answer.
  • David
    David about 11 years
    @ViinQuileste Take one step back, why do you need that? raw_input can be shuttled to subProcess to execute shell commands and using readline would provide tab completion.
  • Viin
    Viin about 11 years
    thanks Mike, but is there a way for an interactive user input in command line form?
  • Mike
    Mike about 11 years
    I'm not sure what you mean, other than raw_input, which is interactive. So you would call the script from the command line, it might execute a few commands first, then it would get to the raw_input line and ask you for input, then continue. I've updated the second section of my answer to give you a fuller example.
  • Viin
    Viin about 11 years
    thanks but still its no in command line form. is this even possible?
  • Mr_Spock
    Mr_Spock about 11 years
    Are you feeding it a file like "input.txt" and you want it to read from that file?
  • Viin
    Viin about 11 years
    So you mean to say that raw_input is the only way to have an input from the user while the program is already running?
  • Mike
    Mike about 11 years
    I think so (unless you import some other big things like GUIs). I'll point out that raw_input is a built-in part of python, and won't be going anywhere, except for the name change in python 3. Is there some reason you don't want to use it? Do you need to parse the input or something?
  • Mike
    Mike about 11 years
    Do you mean you're feeding it the name of a file or directory? Or do you want it to read a file for some arguments you want your program to use? I think we're confused by what you mean when you say "command line form".
  • Viin
    Viin about 11 years
    what i meant when i say "command line form" is the user will input the file or the directory in a black screen. i mean just like "stdin" in c and "cin" in c++.
  • Mr_Spock
    Mr_Spock about 11 years
    The last part is probably what you want.
  • Viin
    Viin about 11 years
    Yes this is a big help. Thanks Mr_Spock. God Bless.
  • Mr_Spock
    Mr_Spock about 11 years
    No problem. Feel free to accept the answer if this is it. haha
  • Mr_Spock
    Mr_Spock about 11 years
    You can take in the file strictly with raw_input(). Not sure why you'd complicate things with sys in this case, although it works. See the second half of my answer to see what I mean.
  • Mr_Spock
    Mr_Spock about 11 years
    Is this the answer? If not, can you clarify your question?
  • Mike
    Mike about 11 years
    Yes, you can take one file as input with redirection, but you cannot take multiple files (though cat could work). At the time I wrote that section of my answer, it wasn't clear what Viin meant; I thought Viin wanted to process command-line arguments. (I still don't understand precisely what Viin wanted...)
  • Mike
    Mike about 11 years
    In any case, both sections are possible answers to possible interpretations to the question.
  • Nishank Singla
    Nishank Singla over 8 years
    How to run a python script "myscript.py" from command prompt as a command as "myscript args1 args2" not as "python myscript.py args1 args2" ?
  • Mike
    Mike over 8 years
    On OS X and linux do chmod +x myscript.py and add #! /usr/bin/env python to the very top of the script file. On windows, read the directions.
  • Dante Puglisi
    Dante Puglisi over 6 years
    This works great actually but it would be nice to have an explanation of what '-i' does
  • James
    James over 5 years
    @DantePuglisi -i means interactive