【问题标题】:Python - Calling popen with many argumentsPython - 使用许多参数调用 popen
【发布时间】:2013-01-24 17:22:29
【问题描述】:

我正在尝试使用参数列表调用 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

TIA 寻求帮助!

编辑

编辑编辑

没关系,我看到了问题。 []...谢谢!呵呵

【问题讨论】:

  • 我也犯了同样的错误! (感谢这个问题节省了 10 分钟)

标签: python exec popen


【解决方案1】:

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)

【讨论】:

  • 附带说明,将整个内容放入一个字符串中,然后在我所见的范围内拆分它真的没有任何优势......
猜你喜欢
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多