相关疑难解决方法(0)

Python Popen:同时写入stdout和日志文件

我正在使用Popen调用一个shell脚本,该脚本不断将其stdout和stderr写入日志文件.有没有办法连续同时输出日志文件(到屏幕),或者让shell脚本同时写入日志文件和标准输出?

我基本上想在Python中做这样的事情:

cat file 2>&1 | tee -a logfile #"cat file" will be replaced with some script
Run Code Online (Sandbox Code Playgroud)

再次,这将stderr/stdout连接到tee,将它写入stdout和我的日志文件.

我知道如何在Python中将stdout和stderr写入日志文件.我被困在哪里是如何将这些复制回屏幕:

subprocess.Popen("cat file", shell=True, stdout=logfile, stderr=logfile)
Run Code Online (Sandbox Code Playgroud)

当然我可以做这样的事情,但有没有办法在没有tee和shell文件描述符重定向的情况下做到这一点?:

subprocess.Popen("cat file 2>&1 | tee -a logfile", shell=True)
Run Code Online (Sandbox Code Playgroud)

python subprocess popen

38
推荐指数
3
解决办法
4万
查看次数

如何在Python中将stdout和stderr重定向到logger

我有一个记录器,有一个RotatingFileHandler.我想重定向所有StdoutStderr该记录器.怎么办?

python logging stdout python-3.x

30
推荐指数
8
解决办法
3万
查看次数

标签 统计

python ×2

logging ×1

popen ×1

python-3.x ×1

stdout ×1

subprocess ×1