Python程序教程

您现在的位置是:首页 >  Python

当前栏目

python 删除文件中的空行

python,删除,文件,空行
2025-03-28 09:01:05 时间

大家好,又见面了,我是你们的朋友全栈君。

#!/usr/bin/python3

# -*- coding: UTF-8 -*-

def Del_line(file_path):

with open(file_path,”r”) as f:

res = f.readlines() #res 为列表

res = [x for x in res if x.split()] #将空行从 res 中去掉

with open(file_path,”w”) as f:

f.write(“”.join(res)) #将 res 转换为 字符串重写写入到文本

return

Del_line(“test.txt”)

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/139168.html原文链接:https://javaforall.cn