>>操作符在python中做什么?

2024-04-25 21:52:20 发布

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

我在一个项目中发现了这段代码,我不知道>>是做什么的。有人解释吗?你知道吗

def save(self, fpath=None):
        """
        Save the JSON data to fpath. This is done automatically if the
        game is over.
        """
        if fpath is None:
            fpath = _jsonf % self.eid
        try:
            print >> gzip.open(fpath, 'w+'), self.rawData,
        except IOError:
            print >> sys.stderr, "Could not cache JSON data. Please " \
                                 "make '%s' writable." \
                                 % os.path.dirname(fpath)

我知道这段代码是从模块中的其他文件和对象获取信息的,我也知道代码的总体工作原理。只有print >>让我困惑。当这个模块安装在没有写访问权限的目录中时,会出现消息Could not cache...。整个文件位于here,但我怀疑它是否有用。你知道吗


Tags: 模块文件the代码selfnonejsoncache
1条回答
网友
1楼 · 发布于 2024-04-25 21:52:20

>>打印到类似文件的对象

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.

^{}

在本例中,它将错误消息打印到stderr

相关问题 更多 >

    热门问题