How to use sys.argv in Python

Posted in /  

How to use sys.argv in Python
vinaykhatri

Vinay Khatri
Last updated on April 19, 2024

    Python comes with a built-in sys module which stands for System-Specific and using this module we can indulge with the Python interpreter and some of its variables. In Python core development we often use the sys module to deal with the operating systems and python interpreter itself.

    Here in this article we will go through the sys module argv property and discuss how to parse command-line options and arguments with this sys.argv property.

    What are command line Arguments in Python?

    Like the concept of function argument or parameters in Python, we have command-line arguments for command line terminal or Python shell. When we execute a python script using terminal or shell, such as by using the Command prompt of windows and terminal of macOS, we use the python command followed by the script name.

    For example C:\>python script.py But Python also allows us to pass arguments while we execute any python script at the terminal, and these arguments are known as Command Line Arguments in Python Example

    C:\>python script.py arg1 arg2 arg3

    here the script.py is the python script and arg1, arg2, and arg3 are Command-Line arguments of Python.

    Python sys.argv property with Examples

    By far we have learned that what are Command Line Arguments in Python and how can we pass them while executing a Python program in a command terminal.

    Now let’s discuss how can we access them within a Python program. The sys.argv property allows us to grab those Command line arguments as a list of strings values. The sys.argv is a Python list that gets automatically created when we execute the Python script using the system terminal or command prompt for windows.

    Example #example.py

    import sys
    print(sys.argv)

    Execute on terminal

    C:\Users\tsmehra\dev >python example.py
    ['example.py']

    And when we execute our python script the name of the script becomes the first element of the sys.argv list.

    Pass Python Command-Line arguments and access them using sys.argv

    As we know that sys.argv is a list of Python Command-line Arguments and by default, its first element is the script name itself. And it takes all the command lines argument as a string value.

    example.py

    import sys
    print(sys.argv)

    Execute at terminal

    C:\Users\tsmehra\dev>python example.py first second third 1 2 3
    ['example.py', 'first', 'second', 'third', '1', '2', '3']

    Why do we use the Command Line Arguments in Python?

    The only purpose of command-line arguments to pass additional information about the program execution at runtime. Using this we can give inputs to the program when we execute it.

    example.py

    import sys
    print("The file name is:", sys.argv[0])
    
    name = sys.argv[1]
    last_name = sys.argv[2]
    
    print("This file is executed by:",name, last_name )

    Execute

    C:\Users\tsmehra\dev>python example.py Ram Kumar
    The file name is: example.py
    This file is executed by: Ram Kumar

    Conclusion

    Here in this article, you learned how can you access the Command Line Arguments in Python using the sys module argv property. The Python sys.argv is a list that gets automatically created when the Python script is executed and by default, the script name becomes the first value of this list. All the other command-line arguments are stored as string values to the sys.argv list.

    People are also reading:

    Leave a Comment on this Post

    0 Comments