I would like to note that the previous answers made a lot of assumptions about user knowledge. This answer attempts to answer the question at a more educational level.
For each Python call, sys.argv automatically represents a list of lines representing arguments (separated by spaces) on the command line. The name comes from a C programming convention in which argv and argc represent command line arguments.
You will want to know more about lists and strings when you get to know Python, but at the same time, here are a few things you need to know.
You can simply create a script that prints the arguments in the form in which they are presented. It also prints the number of arguments using the len function in the list.
from __future__ import print_function import sys print(sys.argv, len(sys.argv))
The script requires Python 2.6 or later. If you call this print_args.py script, you can call it with other arguments to see what happens.
> python print_args.py ['print_args.py'] 1 > python print_args.py foo and bar ['print_args.py', 'foo', 'and', 'bar'] 4 > python print_args.py "foo and bar" ['print_args.py', 'foo and bar'] 2 > python print_args.py "foo and bar" and baz ['print_args.py', 'foo and bar', 'and', 'baz'] 4
As you can see, command line arguments include the name of the script, but not the name of the interpreter. In this sense, Python treats the script as an executable file. If you need to know the name of the executable (in this case python), you can use sys.executable .
As you can see from the examples, you can get arguments containing spaces if the user called the script with arguments enclosed in quotation marks, so that you get a list of arguments provided by the user.
Now in your Python code, you can use this list of strings as input to your program. Since lists are indexed with integers starting at zero, you can get individual items using the syntax list [0]. For example, to get the script name:
script_name = sys.argv[0]
Although this is interesting, you rarely need to know the name of your script. To get the first argument after the script for the file name, you can do the following:
filename = sys.argv[1]
This is a very common use, but note that it will fail with an IndexError if no argument has been provided.
In addition, Python allows you to refer to a fragment of a list, so to get a different list of user-provided arguments only (but without a script name), you can do
user_args = sys.argv[1:]
In addition, Python allows you to assign a sequence of elements (including lists) to variable names. Therefore, if you expect the user to always provide two arguments, you can assign these arguments (as strings) to two variables:
user_args = sys.argv[1:] fun, games = user_args
So, to answer your specific question, sys.argv[1] represents the first command-line argument (as a string ) provided to the script in question. It will not request input, but will fail with an IndexError if no arguments are specified after the script name on the command line.