简体   繁体   English

使用Python将stdout和stderr重定向到同一文件

[英]Redirect stdout and stderr to same file using Python

I would like to redirect the standard error and standard output of a Python script to the same output file. 我想将Python脚本的标准错误和标准输出重定向到相同的输出文件。 From the terminal I could use 从我可以使用的终端

$ python myfile.py &> out.txt

to do the same task that I want, but I need to do it from the Python script itself. 做我想做的同样的任务,但我需要从Python脚本本身做。

I looked into the questions Redirect subprocess stderr to stdout , How to redirect stderr in Python? 我查看了问题Redirect subprocess stderr to stdout如何在Python中重定向stderr? , and Example 10.10 from here , and then I tried the following: ,从这里开始实例10.10,然后我尝试了以下内容:

import sys
fsock = open('out.txt', 'w')
sys.stdout = sys.stderr = fsock

print "a"

which rightly prints the letter "a" in the file out.txt; 正确地在文件out.txt中打印字母“a”; however, when I try the following: 但是,当我尝试以下内容时:

import sys
fsock = open('out.txt', 'w')
sys.stdout = sys.stderr = fsock

print "a   # missing end quote, will give error

I get the error message "SyntaxError ..." on the terminal, but not in the file out.txt. 我在终端上收到错误消息“SyntaxError ...”,但在out.txt文件中没有。 What do I need to do to send the SyntaxError to the file out.txt? 将SyntaxError发送到文件out.txt需要做什么? I do not want to write an Exception, because in that case I have to write too many Exceptions in the script. 我不想写一个Exception,因为在这种情况下我必须在脚本中编写太多的Exceptions。 I am using Python 2.7. 我使用的是Python 2.7。

Update: As pointed out in the answers and comments below, that SyntaxError will always output to screen, I replaced the line 更新:正如下面的答案和评论所指出的,SyntaxError将始终输出到屏幕,我替换了该行

print "a   # missing end quote, will give error

by 通过

print 1/0  # Zero division error

The ZeroDivisionError is output to file, as I wanted to have it in my question. ZeroDivisionError输出到文件,因为我想在我的问题中得到它。

A SyntaxError in a Python file like the above is raised before your program even begins to run: Python files are compiled just like in any other compiled language - if the parser or compiler can't find sense in your Python file, no executable bytecode is generated, therefore the program does not run. 在程序开始运行之前,会引发上述Python文件中的SyntaxError:Python文件的编译方式与任何其他编译语言一样 - 如果解析器或编译器在Python文件中找不到意义,则没有可执行字节码生成,因此程序不运行。

The correct way to have an exception generated on purpose in your code - from simple test cases like yours, up to implementing complex flow control patterns, is to use the Pyton command raise . 在代码中有意生成异常的正确方法 - 从像你这样的简单测试用例到实现复杂的流控制模式,就是使用Pyton命令raise

Just leave your print there, and a line like this at the end: 把你的印刷品留在那里,最后是这样的一条线:

raise Exception

Then you can see that your trick will work. 然后你可以看到你的技巧会起作用。

Your program could fail in runtime in many other ways without an explict raise, like, if you force a division by 0, or simply try to use an unassigned (and therefore "undeclared") variable - but a deliberate SyntaxError will have the effect that the program never runs to start with - not even the first few lines. 你的程序可能在许多其他方面在运行时失败,没有明显的提升,例如,如果你强制除以0,或者只是尝试使用未分配的(因此“未声明的”)变量 - 但是故意的SyntaxError会产生这样的效果:程序永远不会开始 - 甚至不是前几行。

This works 这有效

sys.stdout = open('out.log', 'w')
sys.stderr = sys.stdout

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

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