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

The with statement is excellent for automatically opening and closing files.

with open('myfile','rw') as file:
    for line in file:
        if not line.isspace():
            file.write(line)

Leave a Comment