Answers for "python remove empty new lines"

2

python remove new line

lines = ("line 1 rn")
lines.rstrip("nr")
Posted by: Guest on February-16-2021
0

python remove empty lines from file

import os


def remove_empty_lines(filename):
    if not os.path.isfile(filename):
        print("{} does not exist ".format(filename))
        return
    with open(filename) as filehandle:
        lines = filehandle.readlines()

    with open(filename, 'w') as filehandle:
        lines = filter(lambda x: x.strip(), lines)
        filehandle.writelines(lines)
Posted by: Guest on June-29-2021

Code answers related to "python remove empty new lines"

Python Answers by Framework

Browse Popular Code Answers by Language