Python 3 operator >> to print to file

后端 未结 3 1079
遇见更好的自我
遇见更好的自我 2020-12-29 20:38

I have the following Python code to write dependency files of a project. It works fine with Python 2.x, but while testing it with Python 3 it reports an error.



        
3条回答
  •  一个人的身影
    2020-12-29 20:58

    Note that starting in Python 3.6.3 (September 2017), the error message for this case will be changing to recommend the Python 3 spelling:

    >>> print >> sys.stderr
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: unsupported operand type(s) for >>:
        'builtin_function_or_method' and '_io.TextIOWrapper'.
        Did you mean "print(, file=)"?
    

    (Explicit line breaks added to avoid side-scrolling - the actual error message just wraps at the width of your terminal window)

提交回复
热议问题