赞
踩
在文件夹作用包含许多压缩包的时候,解压起来就很费时费力,尤其是在文件夹还存在嵌套的情况下,解压起来就更麻烦了。Franpper今天给大家带来递归遍历指定路径下的所有文件和文件夹,批量解压所有压缩包的方法,帮大家一键解压。
常见的压缩包格式有rar、zip、7z,Franpper将这几种文件的解压方式都写在了方法里,下面以7z为例为大家详细介绍一下,全部完整代码见最底部。
目录
首先是函数mkdir函数,用来新建文件夹存放解压文件。
- def mkdir(path):
- isExists = os.path.exists(path)
- if not isExists:
- os.makedirs(path)
- print(path + '创建成功')
- else:
- print(path + '目录已存在')
生成unzip_log.txt日志文件,用来记录解压失败的文件路径,这些文件需要手动解压。
wrong_log = os.path.join(folder_path, 'unzip_log.txt')
递归遍历文件夹时,获取文件夹中所有文件夹的名字,如果压缩包的名字与同目录下文件夹(若存在)的名字相同,则认为已经被解压过,不对其进行解压操作。
- contents = os.listdir(root)
- folders = [folder for folder in contents if os.path.isdir(os.path.join(root, folder))]
对于要解压的文件,获取其名字,生成文件夹。
- zipFile_name = file.split('.')[0:-1]
- zipFile_name = '.'.join(zipFile_name)
接下来进行解压操作:
- with py7zr.SevenZipFile(zipFile_path, mode='r') as z:
- z.extractall(path=unzip_zipFile_path)
解压失败的文件路径记录到日志中:
- with open(wrong_log, 'a') as f:
- f.write(f'\n {zipFile_path}')
需要注意的是:
1) 在使用rarfile解压rar文件的时候会出现解压失败的情况,需要将winrar的目录中的UnRAR.exe,拷贝至python脚本目录下。如下图:
2) 使用zipfile加压zip文件的时候会出现解压文件乱码的情况,需要将zipfile.py文件中两处按下图修改。
-
- import os
- import zipfile
- import rarfile
- import py7zr
- """
- 解压文件
- """
-
-
- def mkdir(path):
- isExists = os.path.exists(path)
- if not isExists:
- os.makedirs(path)
- print(path + '创建成功')
- else:
- print(path + '目录已存在')
-
-
- def unzipFile(folder_path):
- wrong_log = os.path.join(folder_path, 'unzip_log.txt')
- for root, dirs, files in os.walk(folder_path):
- contents = os.listdir(root)
- folders = [folder for folder in contents if os.path.isdir(os.path.join(root, folder))] # 该目录下文件夹名称列表
- for file in files:
- if file.endswith('7z'):
- zipFile_name = file.split('.')[0:-1]
- zipFile_name = '.'.join(zipFile_name)
- if zipFile_name in folders:
- continue
- # 没有重名文件则进行解压
- else:
- # 创建解压文件夹路径
- unzip_zipFile_path = os.path.join(root, zipFile_name)
- mkdir(unzip_zipFile_path)
- zipFile_path = os.path.join(root, file)
- print(zipFile_path)
- try:
- with py7zr.SevenZipFile(zipFile_path, mode='r') as z:
- z.extractall(path=unzip_zipFile_path)
- except:
- with open(wrong_log, 'a') as f:
- f.write(f'\n {zipFile_path}')
-
- elif file.endswith('rar'): # file 是待解压文件
- # 有重名文件说明被解压过,跳过
- rarFile_name = file.split('.')[0:-1]
- rarFile_name = '.'.join(rarFile_name)
- if rarFile_name in folders:
- continue
- # 没有重名文件则进行解压
- else:
- # 创建解压文件夹路径
- unzip_rarFile_path = os.path.join(root, rarFile_name)
- mkdir(unzip_rarFile_path)
- rarFile_path = os.path.join(root, file)
- print(rarFile_path)
- try:
- with rarfile.RarFile(rarFile_path) as rar_ref:
- rar_ref.extractall(unzip_rarFile_path)
- except:
- pass
- with open(wrong_log, 'a') as f:
- f.write(f'\n {rarFile_path}')
-
- elif file.endswith('zip'): # file 是待解压文件
- # 有重名文件说明被解压过,跳过
- rarFile_name = file.split('.')[0:-1]
- rarFile_name = '.'.join(rarFile_name)
- if rarFile_name in folders:
- continue
- # 没有重名文件则进行解压
- else:
- # 创建解压文件夹路径
- unzip_rarFile_path = os.path.join(root, rarFile_name)
- mkdir(unzip_rarFile_path)
- rarFile_path = os.path.join(root, file)
- print(rarFile_path)
- try:
- with zipfile.ZipFile(rarFile_path, 'r') as zip_ref:
- zip_ref.extractall(unzip_rarFile_path)
- except:
- with open(wrong_log, 'a') as f:
- f.write(f'\n {rarFile_path}')
- else:
- continue
-
-
- unzipFile(r"G:\work\")
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。