provide arguments to python script code example
Example 1: python read arguments
#!/usr/bin/python
import sys
print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv))
Example 2: read argument from terminal
#!/usr/bin/python
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
#Terminal
# $ python test.py arg1 arg2 arg3
#print
#Number of arguments: 4 arguments.
#Argument List: ['test.py', 'arg1', 'arg2', 'arg3']
Example 3: python arguments
import sys
print ("the script has the name %s" % (sys.argv[0])
Example 4: program arguments python
#!/usr/bin/python
import sys
for args in sys.argv:
print(args)
"""
If you were to call the program with subsequent arguments, the output
will be of the following
Call:
python3 sys.py homie no
Output:
sys.py
homie
no
"""