使用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