2025年6月4日 星期三 乙巳(蛇)年 三月初八 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

用Python批量获取某目录下压缩包中指定文件集

时间:04-26来源:作者:点击数:15

一、提前准备工作

以下两种方式根据你的实际情况二选一

1、如果你使用python没有用到虚拟环境管理工具,就直接把UnRAR.exe文件放在python安装目录下的Scripts文件夹下

2、如果你有使用虚拟环境管理工具,则把UnRAR.exe放到当前你需要使用的虚拟环境下的Scripts文件夹下

二、上代码

  • # _*_ coding: utf-8 _*_
  • import rarfile, os, re, shutil, zipfile
  • from os import listdir
  • from OperateFile import *
  • # param(压缩包所在文件夹的dir,指定压缩包解压后存放文件的位置,要从压缩包中匹配出的文件的文件名数组)
  • # return null
  • # @AuThor wavezhou
  • # @date 20211020
  • class BatchDecompression(object):
  • def __int__(self):
  • super(BatchDecompression, self)
  • def __init__(self, sourcedir, targetdir, sample):
  • self.source = sourcedir # 存放压缩包的目录
  • self.target = targetdir # 输出压缩包中想保留的文件的存放目录
  • self.sample = sample # 匹配出压缩包想保留的文件的匹配模板
  • self.flag = None
  • self.valid_file_list = list()
  • self.origin_files = list()
  • self.save_origin_other_files(sourcedir)
  • def save_origin_other_files(self, sourcedir):
  • for file_or_rar in os.listdir(sourcedir):
  • if os.path.isfile(os.path.join(sourcedir, file_or_rar)) and str(file_or_rar)[-3:] not in (
  • 'rar', 'RAR', 'ZIP', 'zip'):
  • self.origin_files.append(file_or_rar)
  • self.valid_file_list.append(file_or_rar)
  • @staticmethod
  • def compress(file_list, target_path):
  • with zipfile.ZipFile(target_path, 'w', compression=zipfile.ZIP_DEFLATED) as zipf:
  • for file in file_list:
  • zipf.write(file, os.path.basename(file))
  • def batchExt(self, pwd=None):
  • if len(self.sample) < 1:
  • raise Exception('第三个参数数组不能为空')
  • filenames = listdir(self.source)
  • for file in filenames:
  • if '某某**压缩包' in file:# 如果确定某压缩包里没你想要的文件,可将该压缩包文件名里含有的部分文字替换掉这里的“某某**压缩包”
  • continue
  • if str(file)[-3:] not in ('rar', 'RAR', 'ZIP', 'zip') and str(file)[-2:] not in ['7z']:
  • continue
  • path1 = os.path.join(self.source, file)
  • if file.lower().endswith('rar'):
  • try:
  • rf = rarfile.RarFile(path1) # 待解压文件
  • rf.extractall(self.target)
  • rf.close() # 解压指定文件路径
  • except Exception as e:
  • if pwd is None:
  • zf = zipfile.ZipFile(path1)
  • zf.extractall(self.target)
  • else:
  • zf = zipfile.ZipFile(path1, 'r')
  • zf.extractall(self.target, pwd=pwd.encode('utf-8'))
  • zf.close()
  • elif file.lower().endswith('zip'):
  • if pwd is None:
  • zf = zipfile.ZipFile(path1)
  • zf.extractall(self.target)
  • else:
  • zf = zipfile.ZipFile(path1, 'r')
  • zf.extractall(self.target, pwd=pwd.encode('utf-8'))
  • zf.close()
  • elif file.lower().endswith('7z'):
  • import py7zr
  • with py7zr.SevenZipFile(path1, mode='r') as z:
  • z.extractall(self.target)
  • z.close()
  • folders = listdir(self.target)
  • if len(self.sample) == 1:
  • pattern = r"" + self.sample[0] # 指定匹配模板
  • self.flag = True
  • self.recursion_search(folders, pattern)
  • else:
  • for i in range(len(self.sample)):
  • if i == len(self.sample) - 1:
  • self.flag = True # 最后一次遍历清楚压缩包
  • pattern = r"" + self.sample[i]
  • self.recursion_search(folders, pattern)
  • for fd in folders: # 最后清除多余文件夹和文件
  • if os.path.isdir(os.path.join(self.target, fd)):
  • shutil.rmtree(os.path.join(self.target, fd))
  • elif fd not in self.valid_file_list:
  • os.remove(os.path.join(self.target, fd))
  • def recursion_search(self, folders, pattern):
  • foders_tmp = folders[:] # 能避免数组中元素删除造成的指针错位
  • for fd in foders_tmp:
  • if fd.endswith('rar') or fd.endswith('zip') or fd.endswith('7z'):
  • if self.flag:
  • os.remove(os.path.join(self.target, fd))
  • folders.remove(fd)
  • continue
  • elif os.path.isfile(os.path.join(self.target, fd)):
  • if re.search(pattern, fd) and re.search(pattern, fd).group(0) == pattern:
  • self.valid_file_list.append(fd)
  • continue
  • path2 = os.path.join(self.target, fd)
  • self.rec_find(path2, pattern)
  • def rec_find(self, path2, pattern): # 开始递归查找想保留的文件内容
  • file_names = os.listdir(path2)
  • for fs in file_names:
  • if os.path.isdir(os.path.join(path2, fs)):
  • self.rec_find(os.path.join(path2, fs), pattern)
  • elif re.search(pattern, fs) and re.search(pattern, fs).group(0) == pattern:
  • self.valid_file_list.append(fs)
  • shutil.move(os.path.join(path2, fs), os.path.join(self.target, fs))
  • if __name__ == '__main__':
  • # 使用示例
  • bd = BatchDecompression(r"D:\temp_ha",r'D:\temp_ha',['某某账单','.txt','.xlsx','pdf'])
  • bd.batchExt()

主要功能:支持从压缩包中找出你想保留的文件,并清理掉多余文件和原压缩包。

背景思想:开发这个工具类时候大模型还没问世,纯手工码字完成,用到了递归思想,简单暴力。

功能详解:

0、在使用示例main下,第一个参数是压缩包所在的目录,第二个参数是从压缩包中匹配出来的文件所要存放的目录,第三个参数是文件匹配模板列表

1、适配压缩包 rar, zip ,7z 格式的压缩包

2、指定路径下的压缩包,哪怕藏再多层的文件夹,也能通过第3个参数的匹配模板找出来你要保留的文件

3、只会保留匹配模板选中的文件,并输出到第二个参数指定的目录,其他未指定在第三个参数匹配模板列表中的文件都会被删除

4,第一个参数路径下的压缩包在代码执行完后,也会被删除

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐