user3885884 user3885884 - 5 months ago 143
Python Question

Strip blank lines from a text file

I'm trying to remove blank lines from a text file, so if I have for example:

line 1
line 2

line 3


line 4
line 5


The result after processing the file should look like this:

line 1
line 2
line 3
line 4
line 5


I searched stack overflow and this is what I came out with so far:

with open('file.txt', 'r+', encoding='utf-8') as file:
for line in file:
if line.strip():
file.write(line)


This worked perfectly except for one little inconvenient; instead of replacing the original content with the new one, the result is appended to the end of the file like so:

line 1
line 2

line 3


line 4
line 5line 1
line 2
line 3
line 4
line 5


My question is, is there any way I can fix this issue inside the for loop without external storage (storing the result in a file or list and writing it back to the file)? or if there is a better way please share.

I hope my question was clear and thanks.

Answer

So firstly, the output is appended to the file because you open the file in r+ mode and the file pointer is pointing at the end of the file. You won't want to overwrite the file while you're reading it, though, so you really need two files:

with open('infile.txt', 'r', encoding='utf-8') as inFile,\
     open('outfile.txt', 'w', encoding='utf-8') as outFile:
    for line in inFile:
        if line.strip():
            outFile.write(line)