如题:
将文件夹c_b下的多个txt文件合并为1个:
代码如下:
import os
filepath=r'c:\Users\64884\Desktop\combine\c_b'
filenames=[file for file in os.walk(filepath)]
ds=[]
lis=[]
for each in filenames[0][-1]:
with open(filenames[0][0]+'\\'+each,mode='r',encoding='utf-8') as f :#拼接每个txt的文件名
txt=f.readlines()
txt0=[each.split() for each in txt]
ds.append(txt)
for i in ds:
for j in i:
if len(j)>0:
lis.append(j)
with open("2019-2021.txt", 'w',encoding='utf-8') as f:
for i in lis:
f.write(i)