Home > Article > Backend Development > How Do I Access Command Line Arguments in Python?
Accessing Command Line Arguments in Python
Acquiring command line arguments is a crucial aspect when executing Python scripts. This allows you to pass values from the console during execution, enabling dynamic configurations or user input.
Solution:
To retrieve command line arguments in Python, utilize the sys module. Its argv attribute holds a list of strings representing the arguments passed during script invocation.
The following steps guide you through the process:
Example:
Consider the following code that demonstrates argument access:
import sys print(sys.argv)
When you run this script with the command $ python myfile.py var1 var2 var3, the output in the Python console will resemble:
['myfile.py', 'var1', 'var2', 'var3']
The script's name is stored as the first element in the list (myfile.py), followed by the specified arguments.
The above is the detailed content of How Do I Access Command Line Arguments in Python?. For more information, please follow other related articles on the PHP Chinese website!