1. 程式人生 > >從365個檔案中選擇某些檔案並複製

從365個檔案中選擇某些檔案並複製

往資料庫中批量的匯入全年的資料(365個檔案),發現再匯入的過程中某幾個的檔案沒有導進去,現在通過程式從全部檔案中(365個)找到那些沒有匯入的天數。


import time
import shutil, os
import pandas


def export(filename,filename1,filename2):
    file = [str(i)[:10] for i in pandas.read_excel(filename)['日期'].tolist()]
    print(file)
    for Folder_file in sorted([a for a, b, c in os.walk(filename1, topdown=True)][1:],key=lambda x:int(x.split('\\')[-1][:-1])):
        excel_files = sorted(os.listdir(Folder_file), key=lambda x: int(x[-7:-5]))
        excel_files = [Folder_file + '\\' + i for i in excel_files]
        print(excel_files)

        for excel_file in excel_files:
            print(filename2 + '\\'+excel_file[-15:-5]+'.xlsx')

            if excel_file[-15:-5] not in file:
                shutil.copy(excel_file, filename2 + '\\'+excel_file[-15:-5]+'.xlsx')






if __name__ == '__main__':
    first = time.time()
    filename = r'E:\1.xls'
    filename1 = r'Z:\附件2:共享資料\投件快遞員_明細'
    filename2 = r'E:\快遞員需要重新匯入的資料'
    export(filename,filename1,filename2)

    last = time.time()

    print('這件事共用時 %f h' % round((last - first) / 3600, 2))






 

然後 篩選成功~