【发布时间】:2013-03-28 23:04:39
【问题描述】:
我正在使用 os.system() 来运行 python 程序并尝试将其输出记录到文件中。这工作正常。
os.system("myprogram.py -arg1 -arg2 > outputfile.txt")
#something here to kill/cleanup whatever is left from os.system()
#read outputfile1.txt- this output file got all the data I needed from os.system()
问题是 myprogram.py 调用了另一个 python 程序,它为我提供了我需要的输出但没有完成 - 我什至可以看到提示变得不同,如下图所示
当我进入程序的下一行时,有没有办法杀死子进程 我尝试使用 os.system("quit()") 和 subprocess.popen("quit()", shell=False) 并没有做任何事情。
我不能真正使用 exit(),因为它只会一起杀死 python。
顺便说一句,这个档位
f=subprocess.Popen("myprogram.py -arg1 > outputfile.txt") and then
f.communicate() #this just stalls because the child program does not end.
【问题讨论】: