我正在使用Popen调用一个不断将其stdout和stderr写入日志文件的shell脚本。有什么办法可以连续(到屏幕上)同时输出日志文件,或者让shell脚本同时写入日志文件和stdout?Python Popen:同时写入标准输出和日志文件
我基本上想要做这样的事情在Python:
cat file 2>&1 | tee -a logfile #"cat file" will be replaced with some script
同样,这种管标准错误/标准输出一起三通,它都写入到标准输出和我的日志文件。
我知道如何将stdout和stderr写入Python中的日志文件。当我被困在是如何把这些复制到屏幕:
subprocess.Popen("cat file", shell=True, stdout=logfile, stderr=logfile)
当然我可以做这样的事情,但有什么办法做到这一点没有三通和外壳文件描述符重定向?:
subprocess.Popen("cat file 2>&1 | tee -a logfile", shell=True)
相关:[?Python的子进程让孩子的输出到文件和终端(http://stackoverflow.com/q/4984428/4279) – jfs 2013-09-23 17:12:49