当我编译下面的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='')
有人能解释为什么会这样吗?
为我工作!
您能显示发生错误的确切行和输入文件吗?>
可能的原因:如果这部分是某些文件,那么您可能正在使用制表符和空格的组合]
我使用的代码:
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中很重要,如果缩进不正确,则代码将无法运行。您发布的代码应缩进如下。 with
语句需要缩进(就像if
语句或for
循环一样)。