import os filedir = os.getcwd() + "\微信文章\四大名著" print(filedir) filenames = os.listdir(filedir) print(filenames) file=open('文件目录.txt','w',encoding="utf-8") # #向文件中写入字符 # # #先遍历文件名 for filename in filenames: filepath=filedir+'\' filepath=filepath+filename # #遍历单个文件,读取行数 for line in open(filepath,'r+',encoding="utf-8"): file.writelines(line) file.write(' ') file.close()