# -*- coding:utf-8 -*-
import os
import shutil
def copyFile(sourcePath,savePath):
image_end = ['.jpg', '.JPG', '.PNG', '.png', '.jpeg', '.JPEG']
for dir_or_file in os.listdir(sourcePath):
filePath = os.path.join(sourcePath, dir_or_file)
if os.path.isfile(filePath):# 判断是否为文件
if os.path.splitext(os.path.basename(filePath))[1] in image_end:#如果文件是图片,则复制,如果都是同一种图片类型也可以用这句:if os.path.basename(filePath).endswith('.jpg'):
print('this copied pic name is '+ os.path.basename(filePath))# 拷贝jpg文件到自己想要保存的目录下
shutil.copyfile(filePath,os.path.join(savePath,os.path.basename(filePath)))
else:
continue
elif os.path.isdir(filePath): # 如果是个dir,则再次调用此函数,传入当前目录,递归处理。
copyFile(filePath,savePath)
else:print('not file and dir '+os.path.basename(filePath))
if __name__ == '__main__':
sourcePath="/123"
savePath="/567"
copyFile(sourcePath, savePath)