what does sys.argv mean in python

sys.argv in Python stands for "System Argument Values". It is a list in Python, which contains the command-line arguments passed to the script. With sys.argv, one can run a script from the command-line and pass arguments to it.

Here's an example of how to use sys.argv:

main.py
import sys

# print number of command-line arguments passed
print("Number of arguments passed:", len(sys.argv))

# print all command-line arguments
print("Command-line arguments are:", str(sys.argv))
200 chars
8 lines

When you execute the above script by passing some command-line arguments, it will print those arguments along with the total number of arguments.

For example:

main.py
$ python script.py arg1 arg2 arg3
Number of arguments passed: 4
Command-line arguments are: ['script.py', 'arg1', 'arg2', 'arg3']
130 chars
4 lines

In this example, sys.argv[0] is the name of the script itself, and the remaining elements are the arguments passed to it.

related categories

gistlibby LogSnag