1. 程式人生 > >python讀取目錄下的相似檔案進行合併

python讀取目錄下的相似檔案進行合併

 

將這5個資料夾合併到sdf.txt檔案中


import re

from envs.django.Lib import os

if __name__ == '__main__':

f1 = open('d:/englishbook/sdf.txt', 'a+', encoding='UTF-8')
path = 'D:\englishbook'
filelist = os.listdir(path)
print(filelist)
for i in filelist:
an = re.search('^[Tech\d\.txt]+$', str(i))
print(i)
print(an)
if an:
with open('d:/englishbook/'+str(i), 'a+', encoding='UTF-8') as file1:
print('d:/englishbook/'+str(i))
file1 = open('d:/englishbook/'+str(i), 'a+', encoding='UTF-8')
file1.seek(0)
contents = file1.read()
f1.write(contents)