功能
修改 file 路径的文件,使其所有的空行被去除
代码
def delete_space(file):
with open(file) as fr:
textlines = fr.readlines()
with open(file, "w") as fw:
for text in textlines:
if text.split():
fw.write(text)
示例
在打开文本时,处理发现的空白
def open_in():
global content
with open("test.txt") as f:
content = [i.strip() for i in f.readlines()]
if '' in content:
delete_space("test.txt")
open_test()