Reputation: 891
For example - I want to do something like...
python DoublePendulum.py INPUT1 INPUT2
(where INPUT1
and INPUT2
are taken as variable inputs within the DoublePendulum program).
Upvotes: 0
Views: 106
Reputation: 63
$ python test.py one two five something
inside test.py
import sys
print(sys.argv[0:1], sys.argv[1:2], sys.argv[2:3], sys.argv[3:4], sys.argv[4:5])
will output as lists:
['test.py'] ['one'] ['two'] ['five'] ['something']
for my_var in sys.argv:
print(my_var)
will output: as strings
test.py one two five something
I made this function for that returns the parameter you want
def give_me_arg(n):
num = len(sys.argv)
if n >= num:
print("Only enter:>",num,'<-and are this from 0 to ',num-1,':', sys.argv)
return ''
else:
for my_var in sys.argv[n:n+1]:
return my_var
my_var=give_me_arg(3)
Upvotes: 1
Reputation: 14872
$ python test.py arg1 arg2 arg3
In test.py
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
output
Number of arguments: 4 arguments.
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']
Python also provided modules that helps you parse command-line options and arguments. There are the following modules in the standard library:
Upvotes: 5