sys.argv[1] meaning in script

后端 未结 9 1398
野趣味
野趣味 2020-11-22 11:02

I\'m currently teaching myself Python and was just wondering (In reference to my example below) in simplified terms what the sys.argv[1] represents. Is it simpl

相关标签:
9条回答
  • 2020-11-22 11:31

    sys.argv is a list containing the script path and command line arguments; i.e. sys.argv[0] is the path of the script you're running and all following members are arguments.

    0 讨论(0)
  • 2020-11-22 11:35

    I would like to note that previous answers made many assumptions about the user's knowledge. This answer attempts to answer the question at a more tutorial level.

    For every invocation of Python, sys.argv is automatically a list of strings representing the arguments (as separated by spaces) on the command-line. The name comes from the C programming convention in which argv and argc represent the command line arguments.

    You'll want to learn more about lists and strings as you're familiarizing yourself with Python, but in the meantime, here are a few things to know.

    You can simply create a script that prints the arguments as they're represented. It also prints the number of arguments, using the len function on 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 script print_args.py, you can invoke it with different 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, the command-line arguments include the script name but not the interpreter name. In this sense, Python treats the script as the executable. If you need to know the name of the executable (python in this case), you can use sys.executable.

    You can see from the examples that it is possible to receive arguments that do contain spaces if the user invoked the script with arguments encapsulated in quotes, so what you get is the list of arguments as supplied by the user.

    Now in your Python code, you can use this list of strings as input to your program. Since lists are indexed by zero-based integers, you can get the individual items using the list[0] syntax. For example, to get the script name:

    script_name = sys.argv[0] # this will always work.
    

    Although interesting, you rarely need to know your script name. To get the first argument after the script for a filename, you could do the following:

    filename = sys.argv[1]
    

    This is a very common usage, but note that it will fail with an IndexError if no argument was supplied.

    Also, Python lets you reference a slice of a list, so to get another list of just the user-supplied arguments (but without the script name), you can do

    user_args = sys.argv[1:] # get everything after the script name
    

    Additionally, Python allows you to assign a sequence of items (including lists) to variable names. So if you expect the user to always supply two arguments, you can assign those 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) supplied to the script in question. It will not prompt for input, but it will fail with an IndexError if no arguments are supplied on the command-line following the script name.

    0 讨论(0)
  • 2020-11-22 11:36

    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
    0 讨论(0)
  • 2020-11-22 11:41

    sys.argv is a list.

    This list is created by your command line, it's a list of your command line arguments.

    For example:

    in your command line you input something like this,

    python3.2 file.py something
    

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

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

    0 讨论(0)
  • 2020-11-22 11:41

    Adding a few more points to Jason's Answer :

    For taking all user provided arguments : user_args = sys.argv[1:]

    Consider the sys.argv as a list of strings as (mentioned by Jason). So all the list manipulations will apply here. This is called "List Slicing". For more info visit here.

    The syntax is like this : list[start:end:step]. If you omit start, it will default to 0, and if you omit end, it will default to length of list.

    Suppose you only want to take all the arguments after 3rd argument, then :

    user_args = sys.argv[3:]
    

    Suppose you only want the first two arguments, then :

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

    Suppose you want arguments 2 to 4 :

    user_args = sys.argv[2:4]
    

    Suppose you want the last argument (last argument is always -1, so what is happening here is we start the count from back. So start is last, no end, no step) :

    user_args = sys.argv[-1]
    

    Suppose you want the second last argument :

    user_args = sys.argv[-2]
    

    Suppose you want the last two arguments :

    user_args = sys.argv[-2:]
    

    Suppose you want the last two arguments. Here, start is -2, that is second last item and then to the end (denoted by ":") :

    user_args = sys.argv[-2:]
    

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

    user_args = sys.argv[:-2]
    

    Suppose you want the arguments in reverse order :

    user_args = sys.argv[::-1]
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 11:43

    Just adding to Frederic's answer, for example if you call your script as follows:

    ./myscript.py foo bar

    sys.argv[0] would be "./myscript.py" sys.argv[1] would be "foo" and sys.argv[2] would be "bar" ... and so forth.

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

    0 讨论(0)
提交回复
热议问题