user285070 user285070 - 14 days ago 5
Python Question

How to delete all blank lines in the file with the help of python?

For example, we have some file like that:


first line

second line



third line


And in result we have to get:


first line

second line

third line


Use ONLY python

Answer
import fileinput
for line in fileinput.FileInput("file",inplace=1):
    if line.rstrip():
        print line