Python flake8 py错误报告W391(文件末尾没有换行符)

时间:2019-11-10 02:59:38

标签: python pep8 flake8 pyflakes

W391表示文件末尾应该有一个(只有一个)空白行。但是,当文件末尾至少有一个换行符时,flake8将报告错误:

$ cat /tmp/test.py
def hello():
    print('hello')


hello()


$ hexdump -C /tmp/test.py
00000000  64 65 66 20 68 65 6c 6c  6f 28 29 3a 0a 20 20 20  |def hello():.   |
00000010  20 70 72 69 6e 74 28 27  68 65 6c 6c 6f 27 29 0a  | print('hello').|
00000020  0a 0a 68 65 6c 6c 6f 28  29 0a 0a                 |..hello()..|
0000002b

您可以在上方看到实际上文件末尾只有一个空行(0a\n)。但是,当我运行flake8时,出现W391错误:

$ flake8 /tmp/test.py
/tmp/test.py:6:1: W391 blank line at end of file

那是为什么?

1 个答案:

答案 0 :(得分:1)

显然,vim会自动在每个文件中添加换行符,这让我以为最后一个空白行不存在。随着时间的流逝,这种隐含的换行符使我困惑,以为最后两个换行符创建了一个空白行。

因此,警告是正确的。文件末尾应该只有一个\n