首页 > 解决方案 > SyntaxError:打印的语法无效(file=saveto,*args,**kwargs)

问题描述

我正在关注这个repo进行安装files2rouge,然后出现如下错误:

Traceback (most recent call last):
  File "setup_rouge.py", line 7, in <module>
    from files2rouge import settings
  File "/home/cerdas/files2rouge/files2rouge/__init__.py", line 2, in <module>
    from files2rouge.files2rouge import main
  File "/home/cerdas/files2rouge/files2rouge/files2rouge.py", line 17, in <module>
    from files2rouge import utils
  File "/home/cerdas/files2rouge/files2rouge/utils.py", line 16
    print(file=saveto, *args, **kwargs)
              ^
SyntaxError: invalid syntax

这里遇到错误的代码行:

def tee(saveto, *args, **kwargs):
    """Mimic the tee command, write on both stdout and file
    """
    print(args, kwargs)
    if saveto is not None:
        print(file=saveto, *args, **kwargs)

我不知道出了什么问题,也不知道是什么原因saveto。请给我一些见解,谢谢。

标签: pythonsyntax-errorpython-2.xrouge

解决方案


print在 Python 2 中是一个语句,在 Python 3 中才成为一个函数,但是如果你想在 Python 2 中将它用作一个函数,你可以添加:

from __future__ import print_function

在使用该print功能之前。


推荐阅读