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.