Python實(shí)現(xiàn)批量解壓文件夾下所有壓縮包
在文件夾作用包含許多壓縮包的時(shí)候,解壓起來就很費(fèi)時(shí)費(fèi)力,尤其是在文件夾還存在嵌套的情況下,解壓起來就更麻煩了。Franpper今天給大家?guī)磉f歸遍歷指定路徑下的所有文件和文件夾,批量解壓所有壓縮包的方法,幫大家一鍵解壓。
常見的壓縮包格式有rar、zip、7z,F(xiàn)ranpper將這幾種文件的解壓方式都寫在了方法里,下面以7z為例為大家詳細(xì)介紹一下,全部完整代碼見最底部。
一、代碼介紹
首先是函數(shù)mkdir函數(shù),用來新建文件夾存放解壓文件。
def mkdir(path):
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
print(path + '創(chuàng)建成功')
else:
print(path + '目錄已存在')
生成unzip_log.txt日志文件,用來記錄解壓失敗的文件路徑,這些文件需要手動解壓。
wrong_log = os.path.join(folder_path, 'unzip_log.txt')
遞歸遍歷文件夾時(shí),獲取文件夾中所有文件夾的名字,如果壓縮包的名字與同目錄下文件夾(若存在)的名字相同,則認(rèn)為已經(jīng)被解壓過,不對其進(jìn)行解壓操作。
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)
接下來進(jìn)行解壓操作:
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}')
二、注意事項(xiàng)
需要注意的是:

1) 在使用rarfile解壓rar文件的時(shí)候會出現(xiàn)解壓失敗的情況,需要將winrar的目錄中的UnRAR.exe,拷貝至python腳本目錄下。如下圖:



2) 使用zipfile加壓zip文件的時(shí)候會出現(xiàn)解壓文件亂碼的情況,需要將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 + '創(chuàng)建成功')
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
# 沒有重名文件則進(jìn)行解壓
else:
# 創(chuàng)建解壓文件夾路徑
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
# 沒有重名文件則進(jìn)行解壓
else:
# 創(chuàng)建解壓文件夾路徑
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
# 沒有重名文件則進(jìn)行解壓
else:
# 創(chuàng)建解壓文件夾路徑
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\")到此這篇關(guān)于Python實(shí)現(xiàn)批量解壓文件夾下所有壓縮包的文章就介紹到這了,更多相關(guān)Python解壓壓縮包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python修改實(shí)例字符串表示的多種技術(shù)指南
掌握修改實(shí)例字符串表示的技巧,是Python開發(fā)者從初級向中級進(jìn)階的??重要標(biāo)志??,本文將深入探討Python中修改實(shí)例字符串表示的多種技術(shù),從基礎(chǔ)方法到高級技巧,希望對大家有所幫助2025-10-10
TensorFlow神經(jīng)網(wǎng)絡(luò)優(yōu)化策略學(xué)習(xí)
這篇文章主要介紹了TensorFlow神經(jīng)網(wǎng)絡(luò)優(yōu)化策略2018-03-03
python3操作微信itchat實(shí)現(xiàn)發(fā)送圖片
這篇文章主要為大家詳細(xì)介紹了python3操作微信itchat實(shí)現(xiàn)發(fā)送圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
echarts動態(tài)獲取Django數(shù)據(jù)的實(shí)現(xiàn)示例
本文主要介紹了echarts動態(tài)獲取Django數(shù)據(jù)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Pandas實(shí)現(xiàn)Excel文件讀取,增刪,打開,保存操作
Pandas?是一種基于?NumPy?的開源數(shù)據(jù)分析工具,用于處理和分析大量數(shù)據(jù)。本文將通過Pandas實(shí)現(xiàn)對Excel文件進(jìn)行讀取、增刪、打開、保存等操作,需要的可以參考一下2023-04-04

