35

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...
0

4 Answers 4

89

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:].

12
  • thank you, but i need command line param rather than a user prompt. is that possible?
    – Viin
    Apr 23, 2013 at 22:07
  • Oh, yes. I'll change my answer.
    – Mike
    Apr 23, 2013 at 22:08
  • @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.
    – David
    Apr 23, 2013 at 22:09
  • thanks Mike, but is there a way for an interactive user input in command line form?
    – Viin
    Apr 23, 2013 at 22:16
  • 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.
    – Mike
    Apr 23, 2013 at 22:18
13

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.

7
  • thanks but still its no in command line form. is this even possible?
    – Viin
    Apr 23, 2013 at 22:19
  • 1
    Are you feeding it a file like "input.txt" and you want it to read from that file?
    – Mr_Spock
    Apr 23, 2013 at 22:20
  • 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".
    – Mike
    Apr 23, 2013 at 22:28
  • 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++.
    – Viin
    Apr 23, 2013 at 22:38
  • 1
    Yes this is a big help. Thanks Mr_Spock. God Bless.
    – Viin
    Apr 23, 2013 at 22:47
12

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

Python 3 example:

line = input('Enter a sentence:')
1

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

2
  • This works great actually but it would be nice to have an explanation of what '-i' does Dec 22, 2017 at 13:23
  • @DantePuglisi -i means interactive
    – James
    Jan 25, 2019 at 23:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.