背景:文件夹中有几十万的文件,只想得到我想要的文件类型,可以用到这个脚本。
只需两个步骤:
1.把目标文件夹中的指定类型文件提取到新文件夹
2.并在目标文件夹中删除这些指定类型文件(这一步可要可不要)
# -*- coding: utf-8 -*-import osimport sysimport shutildef 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)elif os.path.isfile(new_path):Suffix_name=os.path.splitext(new_path)[1]#if Suffix_name in [".bmp",".gif",".png",".jpg"]:#指定文件后缀名,从而指定文件格式 # 文件类型if Suffix_name in [".xml"]:shutil.copy(new_path,Target_area)#删除文件os.unlink(new_path)#else:#print("there is sth wrong")# 两个路径copy_file('/home/zj/center7tagged','/home/zj/xhz/new')