这段代码是什么意思:“print>>sys.stderr”

2024-04-27 11:33:53 发布

您现在位置:Python中文网/ 问答频道 /正文

print >> sys.stderr, "Error in atexit._run_exitfuncs:"

为什么要在sys.stderr前面打印“>;”?

谢谢。


Tags: runingtstderrsyserrorprintatexit
2条回答

此语法意味着写入文件对象(在本例中为sys.stderr),而不是标准输出。[Link]

在Python 3.0中,print变成一个函数而不是一个语句:[Link]

print("Error in atexit._run_exitfuncs:", file=sys.stderr)

Python documentation

print also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as “print chevron.” In this form, the first expression after the >> must evaluate to a “file-like” object, specifically an object that has a write() method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to None, then sys.stdout is used as the file for output.

相关问题 更多 >