windows 为什么在执行打印语句时出现 IOError: (9, 'Bad file descriptor') 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4230855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:37:56  来源:igfitidea点击:

why am I getting IOError: (9, 'Bad file descriptor') error while making print statements?

pythonwindowsservice

提问by Richard

I am running a python2.5 script on a windows 2003 server as a service. I am getting this error for simple print statments:

我在 Windows 2003 服务器上作为服务运行 python2.5 脚本。对于简单的打印语句,我收到此错误:

IOError: (9, 'Bad file descriptor')

I deleted all the print statements because they were only used for development purposes, but I am unsure why a print statement would cause me any greif. I ran the same script not as a service without any major problems. Just wondering if anyone else has any insight?

我删除了所有打印语句,因为它们仅用于开发目的,但我不确定为什么打印语句会导致我出现任何问题。我运行相同的脚本而不是作为服务没有任何重大问题。只是想知道是否有人有任何见解?

回答by Wolph

You can't print because sys.stdoutis not available when not running as a console session.

您无法打印,因为sys.stdout在不作为控制台会话运行时不可用。

Instead of using printstatements you can consider using the loggingmodule so you can set the loglevel and write all critical things to the system event log.

print您可以考虑使用logging模块而不是使用语句,这样您就可以设置日志级别并将所有关键内容写入系统事件日志。



It should be noted that you can still get it to work (or silently ignore the problem) by doing something like this:

应该注意的是,您仍然可以通过执行以下操作来使其工作(或默默地忽略问题):

To write to a file per output stream:

要写入每个输出流的文件:

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

To write to a single file:

要写入单个文件:

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

Or to silently ignore all print statements:

或者默默地忽略所有打印语句:

import sys
class NullWriter(object):
    def write(self, value): pass

sys.stdout = sys.stderr = NullWriter()

回答by Derrick

In Python 2.x, this is the expected behavior. In this bug report, Christian Heimes explains that it is a design decision:

在 Python 2.x 中,这是预期的行为。在这个错误报告中,Christian Heimes 解释说这是一个设计决定:

I recommend against changing the code so late in the Python 2.7 release cycle. A change in behavior is too confusing. And it's not a bug but a design decision, too. Over five years ago I implement parts of the IO interaction with the operating system for Python 3.0. I deliberately did NOT port modifications to 2.6.

我建议不要在 Python 2.7 发布周期的这么晚才更改代码。行为的改变太令人困惑了。这也不是错误,而是设计决策。五年多前,我为 Python 3.0 实现了与操作系统的部分 IO 交互。我故意没有将修改移植到 2.6。

He also recommends a workaround for obtaining Python 3.x-style print()behavior in Python 2.7:

他还推荐了一种print()在 Python 2.7 中获得 Python 3.x 风格行为的解决方法:

from __future__ import print_function
import sys
if sys.executable.endswith("pythonw.exe"):
    sys.stdout = sys.stdout = None

print("can handle sys.stdout = None just fine.")