IndentationError:unindent不匹配任何外部缩进级别[以0.1s完成]

问题描述 投票:0回答:2

当我编译下面的Python代码时,我得到IndentationError:unindent与任何外部缩进级别都不匹配

with open('test.txt','r') as f:
    import sys
    f_contents= f.read(100)
    print(f_contents,end='')
    f_contents= f.read(100)
    print(f_contents,end='')

有人能解释为什么会这样吗?

python windows file sublimetext3 writing
2个回答
0
投票
为我工作!

您能显示发生错误的确切行和输入文件吗?>

可能的原因:如果这部分是某些文件,那么您可能正在使用制表符和空格的组合]

enter image description here

我使用的代码:

with open('test.txt','r') as f: import sys f_contents= f.read(100) print(f_contents,end='') f_contents= f.read(100) print(f_contents,end='')


0
投票
您的代码缩进在Python中很重要,如果缩进不正确,则代码将无法运行。您发布的代码应缩进如下。 with语句需要缩进(就像if语句或for循环一样)。
© www.soinside.com 2019 - 2024. All rights reserved.