【问题标题】:Looping an executable to get the result from Python script循环可执行文件以从 Python 脚本中获取结果
【发布时间】:2010-03-20 17:39:42
【问题描述】:

在我的 python 脚本中,我需要在 for 循环中调用一个可执行文件,并等待该可执行文件将结果写入“output.xml”。

我如何设法使用 wait() 以及我如何知道我的一个可执行文件何时完成生成结果以获取结果?如何关闭该进程并打开一个新进程以再次调用可执行文件并等待新结果?

    import subprocess
    args = ("bin/bar")
    popen = subprocess.Popen(args)

我需要等待“bin/bar”的输出生成“output.xml”,然后从那里读取它的内容。

    for index, result in enumerate(results):
        myModule.callSubProcess(index)
        #this is where the problem is.
        fileOutput = open("output.xml")
        parseAndStoreInSQLiteFileOutput(index, file)

【问题讨论】:

    标签: python linux executable system-calls


    【解决方案1】:

    Popen.wait() 将使脚本等待直到进程结束。之后不需要终止该进程,因为它已经退出了。

    【讨论】:

      【解决方案2】:

      我认为最简单的方法是使用call

      import subprocess
      retcode = subprocess.call('command', shell=True)
      

      它等待进程终止并将返回码分配给变量。 有关更详细的描述,请参阅 python 文档中的17.1.subprocess - convenience functions。希望能帮助到你。

      【讨论】:

        猜你喜欢
        • 2017-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多