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

用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(需要处理的总目录,所需要的类型的列表要加点,移动的目标文件夹路径)





  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值