【发布时间】:2017-04-23 19:35:29
【问题描述】:
我有一个 python 脚本(称为my_python_script.py),它使用打印一些消息
print message_string
它还使用输出百分比进度
sys.stdout.write(progress_string)
完成后,它会输出一些统计数据,因为我稍后想通过stderr 将这些数据收集到一个文件中,所以我使用
sys.stderr.write(stats_string)
现在,在另一个脚本中,我像这样使用subprocess.check_call() 调用上述脚本
file_obj = open('stats.log', 'a')
check_call(['my_python_script.py'], stderr=file_obj)
file_obj.close()
令我惊讶的是,file_obj 包含来自my_python_script.py 的所有输出,包括print 输出、sys.stdout.write 输出和sys.stderr.write 输出。我预计它只会包含来自sys.stderr.write 的输出。
为什么不是这样? check_call是否默认将stderr重定向到stdout?
如果有一个简单的解决办法,我会是一个快乐的露营者。谢谢。
顺便说一句,我在 Linux Fedora 上使用 Python 2.7。
【问题讨论】:
-
如果您的脚本打印
sys.stdout.fileno()和sys.stderr.fileno()输出相同吗?
标签: python python-2.7 subprocess stdout stderr