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

posted @ 2019-09-17 16:21  迷惑の魔男  阅读(2303)  评论(0编辑  收藏  举报