user3885884
user3885884

Reputation: 395

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.

Upvotes: 1

Views: 3740

Answers (2)

Hani
Hani

Reputation: 1424

here is a working example that do what you are looking for. I am not sure about performance here but for sure it does not use the memory or temp file

writebytecount = 0
readbytecount = 0
with open('file2.txt', 'r+') as file:
    while 1:
        line = file.readline()
        if not line: break
        readbytecount = file.tell()
        file.seek(writebytecount)
        file.write(line)
        if len(line.strip()) != 0:
            writebytecount += len(line)+1
        file.seek(readbytecount)
    file.seek(writebytecount)
    file.truncate(writebytecount)
    file.close()

Upvotes: 2

Will
Will

Reputation: 24689

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)

Upvotes: 1

Related Questions