2015-08-08 41 views
-2

当我运行我的代码时,在for循环行中出现错误。for循环IndentationError:意外缩进

这是错误:

File.readlines(): IndentationError: unexpected indent

这是我的代码:

File = open('/home/shar/Desktop/list','r') 
     for line in File.readlines(): 

有什么想法?

+1

那么,你缩进'for'行。你为什么这么做? – BrenBarn

回答

3

由于您通过赋值获取文件对象,因此不需要缩进for

File = open('/home/shar/Desktop/list','r') 
for line in File.readlines(): 
0

当使用for loop,它应该是这个样子:

for x in y: 
    ... 

你并不需要缩进初始行;只有循环内部包含的行。