如何用python删除文件尾行空行
发布网友
发布时间:2022-04-25 15:36
我来回答
共1个回答
热心网友
时间:2023-10-13 12:43
Python读取一个文本文件,删除文本文件的空行代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def delblankline(infile, outfile):
""" Delete blanklines of infile """
infp = open(infile, "r")o
utfp = open(outfile, "w")
lines = infp.readlines()
for li in lines:
if li.split():
outfp.writelines(li)
infp.close()
outfp.close()
#调用示例
if
__name__ == "__main__":
delblankline("1.txt","2.txt")