Python - 使用多个参数调用popen

1 投票
1 回答
2506 浏览
提问于 2025-04-17 13:33

我正在尝试用一组参数来调用popen。

    execString = "java -jar {} {} {} {} {} {}".format(os.path.join(config.java_root,
                                                                   config.java_jar),
                                               self.canvasSize,
                                               self.flightId,
                                               self.domain,
                                               self.defPath,
                                               self.harPath)
    execStringList = execString.split()
    print execStringList
    subprocess.Popen([execStringList])

execStringList是:

['java', '-jar', '/Users/me/Projects/reporting-test/build/clickunit-0.1.jar', '300x1050', '123', 'dev.me.net', '/Users/me/Projects/reporting-test/src/definitions/300x1050.yml', '/Users/me/Projects/reporting-test/out/01/15112']

根据这个链接:Python OSError: [Errno 2],这个格式是正确的。但是,我遇到了以下错误:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in __init__
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1202, in _execute_child
AttributeError: 'list' object has no attribute 'rfind'

如果我把execString当作一个字符串来处理,我会得到一个不同的错误:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in __init__
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1202, in _execute_child
OSError: [Errno 2] No such file or directory

尽管如果我在终端运行这个命令,它是可以工作的。

$> java -jar /Users/me/Projects/reporting-test/build/clickunit-0.1.jar 300x1050 123 dev.me.net /Users/me/Projects/reporting-test/src/definitions/300x1050.yml /Users/me/Projects/reporting-test/out/01/3727

谢谢大家的帮助!

编辑

编辑编辑

没事了,我找到了问题所在。[]...谢谢!哈哈

1 个回答

6

execStringList已经是一个列表了,所以你可以直接把它传给subprocess.Popen

execString = "java -jar {} {} {} {} {} {}".format(os.path.join(config.java_root,
                                                               config.java_jar),
                                           self.canvasSize,
                                           self.flightId,
                                           self.domain,
                                           self.defPath,
                                           self.harPath)
execStringList = execString.split()
print execStringList
# Pass execStringList directly to Popen
subprocess.Popen(execStringList)

撰写回答