0
我在尝试让子流程做我想做的事情时遇到了一些麻烦。我有一个需要输入文件的测试工具。 testtool.exe -f并在命令提示符下运行时产生像这样的输出。使用Popen在一个流中输出所有东西
12:30:46 INFO Output- Processing arguments
12:30:46 INFO Output- Arguments ok
12:30:46 INFO Output- Doing something
12:30:46 INFO Output- More Stuff
12:30:03 ERROR Output- Error found
12:30:03 INFO Output- Finished
12:30:03 INFO Output- Exiting
,所以我试图用子进程运行在Python的Python 3.4.3这个工具。例如...
proc = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True).communicate()[0].decode('utf-8')
for i in proc.splitlines():
print("> {0}".format(i))
不幸的是,输出不包含'ERROR'行。我想完全按照我从命令提示符得到它的输出,因为我需要对它进行额外的验证。例如出现多少个错误行,它们出现在输出中的哪个位置。因此,将ERROR和INFO行分成不同的流不适合我。
> 12:30:46 INFO Output- Processing arguments
> 12:30:46 INFO Output- Arguments ok
> 12:30:46 INFO Output- Doing something
> 12:30:46 INFO Output- More Stuff
> 12:30:03 INFO Output- Finished
> 12:30:03 INFO Output- Exiting
我也试过
proc = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=STDOUT, shell=True).communicate()[0].decode('utf-8')
这是我最新的轮回..
proc = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
proc_data = proc.communicate()
for i in proc_data[0].splitlines():
print ("INFO> {0}".format(i))
for i in proc_data[1].splitlines():
print ("ERROR> {0}".format(i))
而且,从来没有出现错误行。就像它在某处丢失一样?
为什么'shell = True' – marisbest2
因为我正在构建命令以在别处运行并将其作为一个命令传递给子进程。我的印象是,如果我不想将命令分成可执行文件+参数,我应该使用shell = True。 – John
我总是被告知使用shell = True通常不是要走的路。没有它,它会起作用吗? – marisbest2