# -*- coding: utf-8 -*- ''' 主要用途 先產生所有目錄路徑 Get_FolderDir() 在使用 產生的目錄路徑後 將所有檔案名稱擷取出來放置 Excel Get_File_Dir() ''' import os import os.path import glob def Get_FolderDir(): #指定筆記本路徑 path = 'C:/Users/LinZero/Desktop/FolderDir.txt' #指定寫入筆記本的編碼格式 Dir_file = open(path,'w', encoding='UTF-8') #先放入最基本的路徑 並找出所有路徑在開始判斷檔案大小(最少幾MB) OR 判斷附檔明 DirList = [ '\\\\192.168.31.201\\usb-4t\\' ] #找出所有目錄 for DL in DirList: DirListTemp = os.listdir(DL) for DLT in DirListTemp: #扣除不需要的檔案資料夾 if os.path.isdir(DL+DLT) and DLT != "報表檔案" and DLT != "System Volume Information" and DLT != "@eaDir" and DLT != "$RECYCLE.BIN": DirList.append(DL+DLT+'\\') Dir_file.writelines(DL+DLT+'\\'+"\n") Dir_file.close() def Get_File_Dir(): #透過這邊找尋目錄下的檔案 path = 'C:/Users/LinZero/Desktop/FolderDir.txt' Dir_file = open(path,'r', encoding='UTF-8') #引入 excel from openpyxl import Workbook wb = Workbook() ws = wb.active ws['A1'] = "檔案路徑" ws['B1'] = "檔案大小" ws['C1'] = "檔案名稱" for Dir in Dir_file.readlines(): #濾除不需要的字元 譬如 \n dir = str(Dir).strip() files = glob.glob(dir+'*') files2 =[] #先濾掉不可用的名稱(剃除資料夾跟 系統保留檔案等) for name in files: if os.path.isdir(name) == False and os.path.basename(name) !="System Volume Information": files2.append(name) #分別取出 路徑 檔案大小 檔案名稱 副檔案名稱 name_sz_date = [(os.path.dirname(name), os.path.getsize(name), os.path.basename(name),os.path.splitext(name)[-1] ) for name in files2] for dirr, size, basename , DotName in name_sz_date: ##先判斷大小 在判斷附檔案名稱 在思考要怎麼儲存... if size > 524288000 and (str(DotName).upper() == ".AVI" or str(DotName).upper() == ".DIVX" or str(DotName).upper() == ".MKV" or str(DotName).upper() == ".MOV" or str(DotName).upper() == ".MP4" or str(DotName).upper() == ".TS" or str(DotName).upper() == ".WMV" ): print(dirr, round(size/(1024*1024*1024),2), basename,DotName) # Rows can also be appended ws.append([dirr, round(size/(1024*1024*1024),2), basename]) wb.save("C:/Users/LinZero/Desktop/FileDir_Name.xlsx") Dir_file.close()