提取指定后缀名的文件

提取文件

通过选择提取的文件夹保存的文件夹,以及提取文件的后缀名(多个后缀名用英文逗号,分开)

代码:

import os
import shutil
import easygui as g


def getfile(file_path,save_path,exts):
    all_file = os.listdir(file_path)
    exts_b = exts.split(',')
    for each_file in all_file:
        full_path = os.path.join(file_path,each_file)
        if os.path.isdir(full_path):
            if os.listdir(full_path) == []:
                # os.rmdir(full_path)
                pass
            else:
                getfile(full_path)
        else:
            ex = os.path.splitext(each_file)[1]
            for ext in exts_b:
                if ex.casefold() in [ext]:
                    move(full_path, save_path)
                # else:
                #     os.remove(full_path)
    else:
        return



def move(full_path, save_path):
    head_path, file_name = os.path.split(full_path)
    if not os.path.exists(os.path.join(save_path, file_name)):
        shutil.move(full_path, save_path)
    else:
        name, ex = os.path.splitext(file_name)
        old_name = full_path
        n = 1
        new_name = os.path.join(head_path, name + '(' + str(n) + ')' + ex)
        while True:
            try:
                os.rename(old_name, new_name)
                old_name = new_name
                shutil.move(new_name, save_path)
                break
            except:
                n += 1
                new_name = os.path.join(head_path, name + '(' + str(n) + ')' + ex)


file_path = g.diropenbox("需要寻找的文件夹")
save_path = g.diropenbox("保证至该文件夹")
exts = '.' + g.enterbox("请输入需要获取的文件格式")
getfile(file_path,save_path,exts)
上一篇:文件的查找(递归函数)


下一篇:mysql使用group by查询报错SELECT list is not in GROUP BY clause and contains nonaggregated column...解决方案