shar
shar

Reputation: 1243

for loop IndentationError: unexpected indent

When I run my code, I get an error in the for loop line.

This is the error:

File.readlines(): IndentationError: unexpected indent

And this is my code:

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

Are there any ideas?

Upvotes: 1

Views: 8739

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

You don't need to indent the for since you get the file object by assignment.

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

Upvotes: 4

Zach Gates
Zach Gates

Reputation: 4130

When using a for loop, it should look something like this:

for x in y:
    ...

You don't need to indent the initial line; only the lines contained inside the loop itself.

Upvotes: 0

Related Questions