How do I access command line arguments in Python?


Question

I use python to create my project settings setup, but I need help getting the command line arguments.

I tried this on the terminal:

$python myfile.py var1 var2 var3

In my Python file, I want to use all variables that are input.

1
309
10/5/2015 3:41:14 PM

Accepted Answer

Python tutorial explains it:

import sys

print(sys.argv)

More specifically, if you run python example.py one two three:

>>> import sys
>>> print(sys.argv)
['example.py', 'one', 'two', 'three']
429
8/1/2016 8:36:27 PM

import sys

sys.argv[1:]

will give you a list of arguments (not including the name of the python file)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon