Python脚本子进程无法写入Popen文件

时间:2013-12-02 16:48:47

标签: python

我编写了一个脚本来调用wmic Process并将结果写入文件。

此脚本在交互式解释器中运行良好,但在以python脚本运行时则不行。

'''
args = shlex.split("wmic process")
args = shlex.split("wmic process get executablePath")
print(args)
ic', 'process', 'get', 'executablePath']
a = open("sagol.txt","w")
p = subprocess.Popen(args,stdout = a)
a.close()
p.kill()
'''
import shlex,subprocess
args = shlex.split("wmic Process")
a = open("salam.txt","w")
p = subprocess.Popen(args,stdout = a)
print(p)
a.close()
p.kill()

1 个答案:

答案 0 :(得分:0)

在真正有机会编写输出文件之前,关闭输入流并终止已启动的进程。

尝试等待进程完成而不是杀死它:

import shlex,subprocess
args = shlex.split("wmic Process")
a = open("salam.txt","w")
p = subprocess.Popen(args,stdout = a)
print(p)
p.wait()
a.close()