Skip to content Skip to sidebar Skip to footer

Python: Why Do My Loops On A Certain Text File Read Empty Lines Although The File Is Not Empty?

I was trying to learn and experiment with Python today, and I got into trying to open a text file, reading the lines from it, and then writing those lines in a 2nd text file. After

Solution 1:

You're attempting to loop through the contents of f twice:

forlinein f:

forlinezin f:

... and you can't do it that way. The second loop will exit immediately, because you already read all of the lines; there are none left to read.

If you want to save the lines of a file and loop through them several times, use something like this:

all_lines = f.readlines()

Post a Comment for "Python: Why Do My Loops On A Certain Text File Read Empty Lines Although The File Is Not Empty?"