
[英]python Popen print the output of the external application and save it (log it) at the same time
[英]Python - execute a bash command, print and save the output in the same time [duplicate]
这个问题已经在这里有了答案:
从Python,我想执行一个bash命令(也许与subprocess.Popen()一起使用),并且我想打印输出,并且同时将输出保存到变量或文件中。 此命令返回大量文本,并且需要几个小时才能运行。
如果我将输出重定向到文件,是否可以立即打印此文件中出现的所有新行?
如果要在Python中完成所有操作,而无需使用tee
命令,则可以。 基本上,您需要设置stdout=PIPE
(请参阅子过程文档),然后在其上使用select()
,以便在有大量数据要读取时得到通知。 当有可用数据时,将其读取并将其写入文件并同时打印。 您可以在此处解决类似问题的解决方案: https : //stackoverflow.com/a/12272262/4323
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.