I am just starting with python so I am struggling with a quite simple example. Basically I want pass the name of an executable plus its input via the command line arguments, e.g.:
python myprogram refprogram.exe refinput.txt
That means when executing myprogram, it executes refprogram.exe and passes to it as argument refinput. I tried to do it the following way:
import sys, string, os
print sys.argv
res = os.system(sys.argv(1)) sys.argv(2)
print res
The error message that I get is:
res = os.system(sys.argv(1)) sys.argv(2)
^
SyntaxError: invalid syntax
Anyone an idea what I am doing wrong?
I am running Python 2.7
This line
Is wrong in a couple of ways.
First, sys.argv is a list, so you use square brackets to access its contents:
Second, you close out your parentheses on
os.systemtoo soon, andsys.argv(2)is left hanging off of the end of it. You want to move the closing parenthesis out to the very end of the line, after all of the arguments.Third, you need to separate the arguments with commas, a simple space won’t do.
Your final line should look like this: