1. 程式人生 > >python讀取目錄下的相似文件進行合並

python讀取目錄下的相似文件進行合並

pre 文件夾 path 9.png arch see main contents nts

技術分享圖片

將這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)

python讀取目錄下的相似文件進行合並