📜  从文件python代码示例中删除空行

📅  最后修改于: 2022-03-11 14:45:28.585000             🧑  作者: Mango

代码示例2
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)