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
以后整理规范 import os import codecs filenames=os.listdir(os.getcwd()) out=file("name.txt","w") for filename in filenames: out.write(filename.decode("gb2312").encode("utf-8")) out.close() 将执行文件的当前目录及文件名写入到name.txt文件中,