python 太好用了
這一次我想將子目錄先所有jpg和pdf文件都copy出來放到一個文件夾,在網上找了個copy全部文件的代碼修改一下就搞定了
import os import shutil source_path = os.path.abspath(r'F:\tool\') target_path = os.path.abspath(r'D:\putout') if not os.path.exists(target_path): os.makedirs(target_path) if os.path.exists(source_path): # root 所指的是當前正在遍歷的這個文件夾的本身的地址 # dirs 是一個 list,內容是該文件夾中所有的目錄的名字(不包括子目錄) # files 同樣是 list, 內容是該文件夾中所有的文件(不包括子目錄) for root, dirs, files in os.walk(source_path): for file in files: if file.find('jpg') > -1 or file.find('jpeg') > -1 or file.find('pdf') > -1: src_file = os.path.join(root, file) shutil.copy(src_file, target_path) print(src_file) print('copy files finished!')
參考代碼 https://www.cnblogs.com/dazhan/p/9834979.html
不過做文件類型判斷不嚴謹,應該用filetype
(前不久還說不發隨筆了,打臉)