更新时间:2023-08-25 23:35:04
此行
res = os.system(sys.argv(1))sys.argv )
在以下方式中有错误。
首先,sys.argv是一个列表,所以你使用方括号来访问它的内容:
sys.argv [ 1]
sys.argv [2]
其次, code> os.system 太早了, sys.argv(2)
被挂起来了。
第三,你需要用逗号分隔参数,一个简单的
您的最后一行应该如下所示:
res = os.system(sys.argv [1],sys.argv [2])
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
res = os.system(sys.argv(1)) sys.argv(2)
Is wrong in a couple of ways.
First, sys.argv is a list, so you use square brackets to access its contents:
sys.argv[1]
sys.argv[2]
Second, you close out your parentheses on os.system
too soon, and sys.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:
res = os.system(sys.argv[1], sys.argv[2])