自动化——自动解压(zip、7z、rar)带密码

本数据采用第五届“泰迪杯”数据分析技能赛——A题:竞赛作品的自动评判数据

#https://www.pudn.com/news/62b430eddfc5ee1968969bb7.html
import os
import zipfile
import py7zr
import rarfileclass UnCompress:def __init__(self, file_path, output_path, password=None):self.file_path = file_path                  # 输入文件路径self.output_path = output_path              # 输出文件路径self.password = password                    # 压缩密码# zip解压缩def unzip_file(self):try:with zipfile.ZipFile(file=self.file_path, mode='r') as fp:fp.extractall(self.output_path, pwd=self.password.encode('ascii'))return Trueexcept:return False# 7z解压缩def un7z_file(self):try:with py7zr.SevenZipFile(self.file_path, 'r', password=self.password) as fp:fp.extractall(path=self.output_path)return Trueexcept:return False# RAR解压缩def unrar_file(self):try:with rarfile.RarFile(self.file_path, 'r') as fp:fp.extractall(self.output_path, pwd=self.password)return Trueexcept:return Falsedef run(self):file_state = Falseif not os.path.exists(self.file_path):return file_stateif not os.path.exists(self.output_path):os.makedirs(self.output_path)# zip解压缩if zipfile.is_zipfile(self.file_path):file_state = self.unzip_file()# 7z解压缩if py7zr.is_7zfile(self.file_path):file_state = self.un7z_file()# RAR解压缩if rarfile.is_rarfile(self.file_path):file_state = self.unrar_file()return file_state

循环调用类

import os
import zipfile
import rarfile
yichang=[]
file_dir = "C:/Users/KANG/Desktop/jpy/A题:竞赛作品的自动评判数据/DataA"
file_list = os.listdir(file_dir)for file_name in file_list:  # 循环读文件print("doing", file_name)web='C:/Users/KANG/Desktop/jpy/A题:竞赛作品的自动评判数据/DataA/'w_input=web+file_namew_output=web+'/DataA/'+file_name[0:file_name.find('.')]a=UnCompress(w_input,w_output,False)a.run()

输出结果:批量解压成功。

 参考链接:Python 实现多种压缩格式文件解压(zip、7z、rar)带密码解压缩,安装避坑-pudn.com


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部