使用python實現遍歷一個目錄及其子文件夾,將其中指定格式文件復制到指定文件夾


# coding=gbk

import os
import sys
import shutil

def copy_file(path,Target_area):#傳入需要遍歷的根目錄和需要復制到的區域
path_list=os.listdir(path)
for new_path in path_list:
new_path=os.path.join(path,new_path)

if os.path.isdir(new_path):
copy_file(new_path,Target_area)
#print("目錄")
elif os.path.isfile(new_path):
Suffix_name=os.path.splitext(new_path)[1]
if Suffix_name in [".bmp",".gif",".png",".jpg"]:#指定文件后綴名,從而指定文件格式
shutil.copy(new_path,Target_area)
#print("文件")
else:
print("there is sth wrong")

copy_file(path,Target_area)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM