????????在文件夾作用包含許多壓縮包的時(shí)候,解壓起來(lái)就很費(fèi)時(shí)費(fèi)力,尤其是在文件夾還存在嵌套的情況下,解壓起來(lái)就更麻煩了。Franpper今天給大家?guī)?lái)遞歸遍歷指定路徑下的所有文件和文件夾,批量解壓所有壓縮包的方法,幫大家一鍵解壓。
? ? ? ? 常見的壓縮包格式有rar、zip、7z,F(xiàn)ranpper將這幾種文件的解壓方式都寫在了方法里,下面以7z為例為大家詳細(xì)介紹一下,全部完整代碼見最底部。
目錄
一、代碼介紹
二、注意事項(xiàng)
三、完整代碼
一、代碼介紹
? ? ? ? 首先是函數(shù)mkdir函數(shù),用來(lái)新建文件夾存放解壓文件。
def mkdir(path):
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
print(path + '創(chuàng)建成功')
else:
print(path + '目錄已存在')
? ? ? ? ?生成unzip_log.txt日志文件,用來(lái)記錄解壓失敗的文件路徑,這些文件需要手動(dòng)解壓。
wrong_log = os.path.join(folder_path, 'unzip_log.txt')
?????????遞歸遍歷文件夾時(shí),獲取文件夾中所有文件夾的名字,如果壓縮包的名字與同目錄下文件夾(若存在)的名字相同,則認(rèn)為已經(jīng)被解壓過(guò),不對(duì)其進(jìn)行解壓操作。
contents = os.listdir(root)
folders = [folder for folder in contents if os.path.isdir(os.path.join(root, folder))]
?????????對(duì)于要解壓的文件,獲取其名字,生成文件夾。
zipFile_name = file.split('.')[0:-1]
zipFile_name = '.'.join(zipFile_name)
?????????接下來(lái)進(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í)候會(huì)出現(xiàn)解壓失敗的情況,需要將winrar的目錄中的UnRAR.exe,拷貝至python腳本目錄下。如下圖:
? ? ? ? 2) 使用zipfile加壓zip文件的時(shí)候會(huì)出現(xiàn)解壓文件亂碼的情況,需要將zipfile.py文件中兩處按下圖修改。
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-703652.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-703652.html
三、完整代碼
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
# 沒(méi)有重名文件則進(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 是待解壓文件
# 有重名文件說(shuō)明被解壓過(guò),跳過(guò)
rarFile_name = file.split('.')[0:-1]
rarFile_name = '.'.join(rarFile_name)
if rarFile_name in folders:
continue
# 沒(méi)有重名文件則進(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 是待解壓文件
# 有重名文件說(shuō)明被解壓過(guò),跳過(guò)
rarFile_name = file.split('.')[0:-1]
rarFile_name = '.'.join(rarFile_name)
if rarFile_name in folders:
continue
# 沒(méi)有重名文件則進(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]批量解壓文件夾下所有壓縮包(rar、zip、7z)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!