【问题标题】:Flake8 E901 unexpected behavior with print() statementFlake8 E901 print() 语句的意外行为
【发布时间】:2019-10-31 06:07:56
【问题描述】:

print() 函数与file 关键字参数一起使用会触发flake8 语法错误(参见下面的示例)。这怎么可能?

# -*- coding: utf-8 -*-
"""Flake8 problem."""
with open('hello_world.txt', 'rw') as f:
    print('Hello, World!', file=f)

E901 SyntaxError: 无效语法 (print('Hello, World!', file=f))

我在 MacOS Catalina 上使用 Sublime Text 3.2.2 和flake8-3.7.9

【问题讨论】:

    标签: python flake8


    【解决方案1】:

    您应该阅读documentation

    file 参数必须是具有write(string) 方法的对象;如果不存在或None,将使用sys.stdout

    以上表示必须是文件对象,而不是文件名,例如:

    with open('hello_world.txt', 'w+') as f:
        print('Hello, World!', file=f)
    

    【讨论】:

    • 添加到@Selcuk,问题不是 flake8 错误,而是 Python 本身。所有 Flake8 错误都以“F”开头。
    • 我正在使用 python 3.7.3
    • 我很确定您安装的 flake8 出于某种原因正在使用 Python2。
    • 我明白了,这确实看起来是一个 Sublime Text 问题(请参阅github.com/SublimeLinter/SublimeLinter-flake8/issues/40)。我要试试这个,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多