Sys.argv [1], which means script

I am currently teaching myself Python, and I'm just wondering (regarding my example below) what sys.argv[1] . Is it just asking for input?

 #!/usr/bin/python3.1 # import modules used here -- sys is a very standard one import sys # Gather our code in a main() function def main(): print ('Hello there', sys.argv[1]) # Command line args are in sys.argv[1], sys.argv[2] .. # sys.argv[0] is the script name itself and can be ignored # Standard boilerplate to call the main() function to begin # the program. if __name__ == '__main__': main() 
+104
python
Nov 07 '10 at
source share
9 answers

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] # this will always work. 

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:] # get everything after the script name 

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 # len(user_args) had better be 2 

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.

+266
Nov 07 '10 at 14:26
source share

sys.argv [1] contains the first command line argument passed to your script.

For example, if your script is named hello.py , and you issue:

  $ python3.1 hello.py foo 

or

 $ chmod +x hello.py # make script executable $ ./hello.py foo 

Your script will print:

  Hello there foo 
+29
Nov 07 '10 at 11:44
source share

sys.argv is a list.

This list is created by your command line, this is a list of command line arguments.

For example:

at the command line you enter something like this,

 python3.2 file.py something 

sys.argv will become a list ['file.py', 'something']

In this case, sys.argv[1] = 'something'

+18
Mar 25 '13 at 1:34
source share

Just adding Frederick to the answer, for example, if you call your script like this:

./myscript.py foo bar

sys.argv[0] will be "./myscript.py" sys.argv[1] will be "foo" and sys.argv[2] will be "bar" ... etc.

In your code example, if you call the script as follows ./myscript.py foo , the script output will be "Hello there foo".

+11
Nov 07 '10 at 11:59 on
source share

Adding a few more comments to Jason Answer:

To accept all the arguments provided by the user: user_args = sys.argv[1:]

Consider sys.argv as a list of strings as (mentioned by Jason). Thus, all manipulations with lists will be applied here. This is called "List Cutting." For more information, visit here .

The syntax looks like this: list [start: end: step]. If you omit the beginning, the default will be 0, and if you omit the end, the list will be specified by default.

Suppose you only want to accept all arguments after the third argument, and then:

 user_args = sys.argv[3:] 

Suppose you only need the first two arguments, and then:

 user_args = sys.argv[0:2] or user_args = sys.argv[:2] 

Suppose you need arguments 2 through 4:

 user_args = sys.argv[2:4] 

Suppose you need the last argument (the last argument is always -1, so what happens here, we start the countdown from the other side. So, the beginning is the last, without end, without step):

 user_args = sys.argv[-1] 

Suppose you need the second last argument:

 user_args = sys.argv[-2] 

Suppose you need the last two arguments:

 user_args = sys.argv[-2:] 

Suppose you need the last two arguments. Here the beginning is -2, this is the second last element, and then to the end (indicated by the symbol ":"):

 user_args = sys.argv[-2:] 

Suppose you want everything but the last two arguments. Here start is 0 (default), and end is the second last element:

 user_args = sys.argv[:-2] 

Suppose you need arguments in reverse order:

 user_args = sys.argv[::-1] 

Hope this helps.

+7
Oct. 16 '13 at 10:20
source share

sys.argv is a list containing the path and command line arguments of the script; those. sys.argv [0] is the path to the script you are using, and all of the following members are arguments.

+3
Nov 07 2018-10-11T00-11-07
source share

To pass arguments to your python script when running the script through the command line

python create_thumbnail.py test1.jpg test2.jpg

here, the script name is create_thumbnail.py, argument 1 is test1.jpg, argument 2 is test2.jpg

Using create_thumbnail.py script I use

 sys.argv[1:] 

which give me a list of arguments passed on the command line, like ['test1.jpg', 'test2.jpg']

+3
Jun 25 '16 at 17:39
source share

sys .argv will display the command line arguments passed when the script was run, or you can say that sys.argv will store the command line arguments passed to python while working from the terminal.

Just try the following:

 import sys print sys.argv 

argv stores all the arguments passed in a python list. The above will print all the arguments passed, the script will work.

Now try running the file filename.py as follows:

 python filename.py example example1 

this will print 3 arguments in a list.

 sys.argv[0] #is the first argument passed, which is basically the filename. 

Similarly, argv 1 is the first argument passed, in this case an "example"

A similar question has already been asked here . Hope this helps!

0
Sep 11 '17 at 1:39 on
source share

sys.argv is an attribute of the sys module. It says that the arguments are passed to the file on the command line. sys.argv[0] catches the directory where the file is located. sys.argv[1] returns the first argument passed to the command line. Think we have an example.py file.

example.py

 import sys # Importing the main sys module to catch the arguments print(sys.argv[1]) # Printing the first argument 

Now here on the command line when we do this:

 python example.py 

It will throw an index error on line 2. Because the argument has not yet been passed. You can see the length of the arguments passed by the user using if len(sys.argv) >= 1: # Code . If we run example.py with argument passing

 python example.py args 

This prints:

 args 

Because it was the first argument! Suppose we made it an executable file using PyInstaller. We would do this:

 example argumentpassed 

This prints:

 argumentpassed 

This is really useful when you make a command in a terminal. First check the length of the arguments. If no arguments are provided, make a help text.

0
Jan 01 '19 at 8:01
source share



All Articles