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...
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:
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.)
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
$ 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.
Suppose you have a script named
foo.py and want to call it with arguments
baz from the command line like
$ foo.py bar baz
$ represents the command-line prompt.) Then, you can do that with the following in your script:
import sys arg1 = sys.argv arg2 = sys.argv
Here, the variable
arg1 will contain the string
arg2 will contain
'baz'. The object
sys.argv is just a list containing everything from the command line. Note that
sys.argv is the name of the script. And if, for example, you just want a single list of all the arguments, you would use