I’m able to pass ints and chars as Python command-line args, but can’t pass Strings successfully to the following:
if __name__ == '__main__' :
try :
print(len(sys.argv))
arg1 = ast.literal_eval(sys.argv[1])
arg2 = ast.literal_eval(sys.argv[2])
print(arg1)
print(arg2)
except Exception :
print('error')
The following works:
python test.py 1 2
and prints:
3
1
2
as dos the following:
python test.py ‘1’ ‘2’
which also prints
3
1
2
However, the following does not work:
python test.py ‘one’ ‘two’
It sees the number of args, but throws an Exception trying to grab them:
3
error
Could someone please provide some guidance on how to pass Strings as command-line args?
Thanks.
ast.literalevalthem. What makes you think that you should do that?