# -*- coding:utf-8 -*- #將全部TXT鏈接成一個,並刪除掉重複記錄 import os import time def BianLi(rootDir): print "開始拼接數據,請稍等..." start_time = time.time() fh = open('C://Users//Administrator//PycharmProjects//···//ALL.txt', "w") list1=[] for root,dirs,files in os.walk(rootDir): for filename in files: #這裏獲得的filename只是一個文件名的字符串而已,如:test.txt filepath = rootDir+filename #拼接目錄和文件名獲得完整路徑 file = open(filepath, 'r') for i in file: #i即爲file中的一行,不用再readline()了 line = str(i).strip() print str(line) if line in list1: #判斷list裏面是否有這個記錄了,若是沒有就加入list,若是有就跳過 continue else: list1.append(line) #向list裏添加記錄 fh.write(line+'\n') fh.close() end_time = time.time() print "所有數據拼接完畢,用時%.2f秒" % (end_time - start_time) rootDir = 'C://Users//Administrator//PycharmProjects//···//test//' BianLi(rootDir) #調用方法