繁体   English   中英

如何在仍打印到子进程的控制台的同时将子进程的输出重定向到文件?

[英]How can I redirect subprocess's output to a file while still printing to the subprocess's console?

我有一个应用程序,在这个应用程序中我运行一个单独的 Windows 应用程序,它产生自己的终端。 当这个单独的应用程序运行时,我可以将输出重定向到一个文件,如下所示

process = subprocess.Popen([self.app, self.app_s],
                           stdout=subprocess.PIPE)

for line in iter(process.stdout.readline, ''):
   print(line)    
   file.write(line)

process.wait()

但是,如果我这样做,我将无法再打印到子进程的控制台,而只能打印到主应用程序的控制台。

有没有办法可以继续在子进程的控制台中查看输出,同时将输出记录到文件中?

通过以下方式,您可以写入所有您想要的地方。

process = subprocess.Popen([self.app, self.app_s],
                           stdout=subprocess.PIPE)
    
for line in proc.stdout:
        sys.stdout.write(line)
        file.write(line)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM