你可以将subprocess.Popen的输出附加到一个文件中,我每天都在使用它.这是我如何做到的:
log = open('some file.txt', 'a') # so that data written to it will be appended
c = subprocess.Popen(['dir', '/p'], stdout=log, stderr=log, shell=True)
(当然这是一个虚拟的例子,我没有使用subprocess来列出文件…)
顺便说一句,任何具有write()的对象都可以替换这个日志项目,所以你可以缓冲输出,并且在这个类似文件的对象里面做任何你想要的(写入文件,显示等).
注意:什么可能是误导,事实上,由于某些原因,我不明白的子流程将会在您想要写入之前写入.所以,这里是使用这个方法:
log = open('some file.txt', 'a')
log.write('some text, as header of the file\n')
log.flush() #
c = subprocess.Popen(['dir', '/p'], stdout=log, stderr=log, shell=True)
所以提示是:不要忘了冲洗输出!