Python popen命令。 等到命令完成

我有一个脚本,用popen shell命令启动。 问题在于脚本不会等到popen命令完成,然后马上继续。

om_points = os.popen(command, "w") ..... 

我怎么能告诉我的Python脚本等待,直到shell命令完成?

根据你想要如何工作你的脚本你有两个select。 如果你想让命令在执行时被阻塞而不做任何事情,你可以使用subprocess.call

 #start and block until done subprocess.call([data["om_points"], ">", diz['d']+"/points.xml"]) 

如果你想在执行的时候做一些事情,或者把东西写入stdin ,你可以在popen调用之后使用communicate

 #start and process things, then wait p = subprocess.Popen([data["om_points"], ">", diz['d']+"/points.xml"]) print "Happens while running" p.communicate() #now wait plus that you can send commands to process 

如文档中所述, wait可能会死锁,所以build议进行通信。

你正在寻找的是wait方法。

你可以使用subprocess来实现这一点。

 import subprocess #This command could have multiple commands separated by a new line \n some_command = "export PATH=$PATH://server.sample.mo/app/bin \n customupload abc.txt" p = subprocess.Popen(some_command, stdout=subprocess.PIPE, shell=True) (output, err) = p.communicate() #This makes the wait possible p_status = p.wait() #This will give you the output of the command being executed print "Command output: " + output