用python把文件夹下所有包含指定后缀的文件移动到指定文件夹

#!/usr/bin/env python
# coding: utf-8
import shutil
import os

def get_all_path(open_file_path):
    rootdir = open_file_path
    path_list = []
    list1 = os.listdir(rootdir)  # 列出文件夹下所有的目录与文件
    for i in range(0, len(list1)):
        com_path = os.path.join(rootdir, list1[i])
        #print(com_path)
        if os.path.isfile(com_path):
            path_list.append(com_path)
        if os.path.isdir(com_path):
            path_list.extend(get_all_path(com_path))
    #print(path_list)
    return path_list
# get all file path under the given folder

def unique_postfix(raw_list):
    p = []
    oo = []
    for i in raw_list:
        m = os.path.splitext(i)
        p.append(m[1])
    mm = tuple(set(p))
    for i in mm:
        oo.append(i)
    return oo

def videotype_in_given(all_suffix):
    normal_with_point = ['.avi','.mov','.rmvb','.mp4','.mpeg','.asf','.wmv','.navi','.3pg','.mkv','.flv','.f4v']
    vdolist = []
    for i in all_suffix:
        if i in normal_with_point:
            vdolist.append(i)
    return vdolist

def shelter_to_given(all_suffix,needed_tpye_list):  #needed_type_list are like['.xlsx','.txt']
    needed = []
    for i in all_suffix:
        if i in needed_tpye_list:
            needed.append(i)
    return needed

def select_given_appedx(all_file_path,appdx):
    hh = []
    for i in all_file_path:
        for a in appdx:
            if a in i:
                hh.append(i)
    return hh

def movfile(file_path_list,to_path):
    for i in file_path_list:
        shutil.move(i,to_path)
############################################
def move_needed_postfix_file(big_path,shelter_list,to_the_path):
    allorgpath = get_all_path(big_path)
    alluniqtype = unique_postfix(allorgpath)
    needtype = shelter_to_given(alluniqtype,shelter_list)
    all_file_path_after_selected = select_given_appedx(allorgpath,needtype)
    movfile(all_file_path_after_selected,to_the_path)

####   move_needed_postfix_file(需要处理的总目录,所需要的类型的列表要加点,移动的目标文件夹路径)





Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐