在继续Python之前,等待子进程.exe完成

时间:2016-04-17 18:28:55

标签: python subprocess wait popen

我在我的代码中运行了一个应用程序,它重写了我稍后需要在代码中阅读的文件。没有输出直接进入我的程序。我无法让代码等到子进程完成,它只是继续并读取未更改的文件。

我已经尝试过subprocess.Popen.wait(),subprocess.call()和subprocess.check_call(),但它们都不能解决我的问题。有谁知道如何使这项工作?感谢。

编辑:这是我的代码的相关部分:

    os.chdir('C:\Users\Jeremy\Documents\FORCAST\dusty')
    t = subprocess.Popen('start dusty.exe', shell=True)
    t.wait()      
    os.chdir('C:\Users\Jeremy\Documents\FORCAST')

2 个答案:

答案 0 :(得分:0)

您是否使用subprocess.Popen()的返回对象?

p = subprocess.Popen(command)
p.wait()

应该有用。

您确定该命令不会立即结束吗?

答案 1 :(得分:0)

如果使用

执行程序
t = subprocess.Popen(prog, Shell=True)

无论程序是否存在,Python都不会抛出错误。如果您尝试使用Popen和Shell = False启动不存在的程序,则会出现错误。我的猜测是你的程序要么不存在于文件夹中,要么不执行。尝试使用Shell = False在Python IDLE环境中执行,看看是否有新窗口。