Using For in else

Hey guys whats up

Whats wrong ? Why cant I print for when its inside else ? Whereas it works fine using the same file when theres no else involved

Hi and welcome to the forum!

It is difficult to work with screenshots, but I think that you may need to reset your position in the file.

When you use readlines you change the file object’s position. To reset the position, you should use, whence).


If you open, you need to remember to close. You need to use


at the end of the cell when you are done working with your file. But if you use the with open style, it will automatically close the file for you.

with open('filename','r') as  f:
   lines = len([line for line in f])
   line_count = len(lines)
#if else part