用python实现文件读取和内容替换

时间:2021-07-18 09:08:00
infile = open("D:/test.txt", "r")  #打开文件
outfile = open("D:/pp2.txt", "w") # 内容输出
for line in infile: #按行读文件,可避免文件过大,内存消耗
outfile.write(line.replace(' ', ' '))#first is old ,second is new
infile.close() #文件关闭
outfile.close()

相关文章