批量提取文件夹下同类型文件/批量提取文件夹下不同名称文件夹的同类型文件

一、背景

        公司服务器有点卡,持续集成分了很多批,有很多文件夹里的html报告需要查看,一个一个点开文件夹比较影响速度

二、需求

        批量提取文件夹下不同文件夹里html文件,并挪到一个新文件夹存储

三、优化

  1. 批量提取文件夹下同类型文件,并挪到一个新文件夹存储
  2. 批量提取文件夹1/文件夹2/…../文件夹n下的同类型文件,并挪到一个新文件夹存储

四、代码

import os
import shutil


def take_samefile(or_path, tar_path, tar_type):
    if not os.path.exists(tar_path):
        os.makedirs(tar_path)
    files = os.listdir(or_path)  # 读取or_path文件列表
    for file in files:
        real_url = os.path.join(or_path, file)
        if os.path.isfile(real_url):  # 文件夹下直接是文件
            file_type = str(file).split('.')[1]  # 读取文件后缀
            if file_type == tar_type:
                print("take{}from{}".format(file, files))
                dir = or_path + '\\' + file  # 存储文件路径
                deter = tar_path + '\\' + str(file)
                shutil.copyfile(dir, deter)
        elif os.path.isdir(real_url):
            take_samefile(real_url, tar_path, tar_type)
        else:
            print("其他情况")
            pass


if __name__ == '__main__':
    print("此工具------Make By 陈哆肉&J")
    print(R"  例如   D:\test\new")
    OldFilePath = str(input(r"请输入存储文件路径:"))
    NewFilePath = str(input(r"请输入新文件路径:"))
    FileType = str(input("请输入文件类型:"))
    # take_samefile(r"D:\test\new", r"D:\新建文件夹", "html")
    take_samefile(OldFilePath, NewFilePath, FileType)

五、打包语句

Pyinstaller -F  -i logo.ico ExctractFiles.py  #打包指定exe图标打包

六、运行截图

《批量提取文件夹下同类型文件/批量提取文件夹下不同名称文件夹的同类型文件》

七、结果

一个新建文件夹下存放了很多html (因公司真名水印不方便截全屏)

 整个过程创作、调试都不易,多多点赞收藏吧

    原文作者:陈哆肉&J
    原文地址: https://blog.csdn.net/qq_39184753/article/details/124381717
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞