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.x风格的print()行为的解决方法:
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.")
无法打印,因为
sys.stdout
不作为控制台会话运行时不可用。不要使用
print
语句,您可以考虑使用logging
模块,这样您就可以设置日志级别,并将所有关键内容写入系统事件日志。需要注意的是,您仍然可以通过执行以下操作来让它正常工作(或默默地忽略问题):
要在每个输出流中写入文件,请执行以下操作:
要写入单个文件:
或者忽略所有打印语句:
在Python2.x中,这是预期的行为。在this bug report中,Christian Heimes解释说这是一个设计决策:
他还建议在Python 2.7中获得Python 3.x风格的
print()
行为的解决方法:相关问题 更多 >
编程相关推荐