Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is a commandline argument?

Tags:

python

I am a newcomer to python (also very sorry if this is a newb question but) i have no idea what a command line argument is. when sys.argv is called, what exactly are the arguments? Any help with understanding this would be a great service.

like image 496
dopatraman Avatar asked Dec 01 '25 08:12

dopatraman


1 Answers

Try running this program:

import sys
print(sys.argv)

You should see results similar to this:

% test.py
['/home/unutbu/pybin/test.py']
% test.py foo
['/home/unutbu/pybin/test.py', 'foo']
% test.py foo bar
['/home/unutbu/pybin/test.py', 'foo', 'bar']
% python test.py foo
['test.py', 'foo']

So, you see sys.argv is a list. The first item is the path to (or filename of) the script being run, followed by command-line arguments.

like image 80
unutbu Avatar answered Dec 05 '25 18:12

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!