一、解壓 zip 文件
基本解壓操作
import zipfile
'''
基本格式:zipfile.ZipFile(filename[,mode[,compression[,allowZip64]]])
mode:可選 r,w,a 代表不同的打開文件的方式;r 只讀;w 重寫;a 添加
compression:指出這個 zipfile 用什么壓縮方法,默認(rèn)是 ZIP_STORED,另一種選擇是 ZIP_DEFLATED;
allowZip64:bool型變量,當(dāng)設(shè)置為True時(shí)可以創(chuàng)建大于 2G 的 zip 文件,默認(rèn)值 True;
'''
zip_file = zipfile.ZipFile(path)
zip_list = zip_file.namelist() # 得到壓縮包里所有文件
for f in zip_list:
zip_file.extract(f, folder_abs) # 循環(huán)解壓文件到指定目錄
zip_file.close() # 關(guān)閉文件,必須有,釋放內(nèi)存
其他方法
zipfile.is_zipfile('xxx.zip') # 判斷文件是否是個有效的zipfile
zipfile.namelist('xxx.zip') # 列表,存儲zip文件中所有子文件的path(相對于zip文件包而言的)
zipfile.infolist('xxx.zip') # 列表,存儲每個zip文件中子文件的ZipInfo對象
zipfile.printdir() # 打印輸出zip文件的目錄結(jié)構(gòu),包括每個文件的path,修改時(shí)間和大小
zipfile.open(name[,mode[,pwd]]) # 獲取一個子文件的文件對象,可以對其進(jìn)行read,readline,write等操作
zipfile.setpassword(psw),為zip文件設(shè)置默認(rèn)密碼
zipfile.testzip() # 讀取zip中的所有文件,驗(yàn)證他們的CRC校驗(yàn)和。返回第一個損壞文件的名稱,如果所有文件都是完整的就返回None
zipfile.write(filename[,arcname[,compression_type]]) # 將zip外的文件filename寫入到名為arcname的子文件中(當(dāng)然arcname也是帶有相對zip包的路徑的),打開方式為w或a
zipfile.extract(member, path=None, pwd=None) # 解壓一個zip中的文件,path為解壓存儲路徑,pwd為密碼
zipfile.extractall(path[,pwd]) # 解壓zip中的所有文件,path為解壓存儲路徑,pwd為密碼
二、解壓rar文件
Python 本身不支持 rar 文件的解壓,需要先安裝相關(guān)依賴才可使用
1.使用rarfile解壓rar文件
pip3 install rarfile 安裝rarfile庫
(注意是解壓,壓縮這個方法不支持)
#coding=utf-8
import rarfile
path = "E:\\New\\New.rar"
path2 = "E:\\New"
rf = rarfile.RarFile(path) #待解壓文件
rf.extractall(path2) #解壓指定文件路徑
rf.extractall(path2,pwd=password) #指定密碼
rf.close()
壓縮包中存在多個文件時(shí),可以使用for循環(huán)進(jìn)行批量解壓文章來源:http://www.zghlxwxcb.cn/news/detail-745655.html
rf = rarfile.RarFile(_rarfile, mode='r') # mode的值只能為'r'
rf_list = rf.namelist() # 得到壓縮包里所有的文件
print('rar文件內(nèi)容', rf_list)
for f in rf_list:
rf.extract(f, folder_abs) # 循環(huán)解壓,將文件解壓到指定路徑
# 一次性解壓所有文件到指定目錄
# rf.extractall(path) # 不傳path,默認(rèn)為當(dāng)前目錄
2、使用unrar解壓 rar 文件
- 安裝 unrar 模塊:pip install unrar
- 下載安裝 unrar library,網(wǎng)址:http://www.rarlab.com/rar/UnRARDLL.exe 按照默認(rèn)安裝路徑安裝
- 將安裝后文件夾中的 X64 文件夾加入環(huán)境變量(默認(rèn)路徑為 C:\Program Files (x86)\UnrarDLL\x64)
- 系統(tǒng)變量中新建變量,變量名輸入 UNRAR_LIB_PATH,變量值為 C:\Program Files(x86)\UnrarDLL\x64\UnRAR64.dll(32位系統(tǒng)下的變量值為C:\Program Files
(x86)\UnrarDLL\UnRAR.dll) - 將 winrar 的目錄下的 unrar.exe 復(fù)制到 Python 路徑的Scripts 文件夾下
- 重啟Python編輯器使環(huán)境變量生效
3、調(diào)用winrar來解壓 rar 文件
當(dāng)上述方法無效時(shí),可以采用os.system執(zhí)行cmd命令的方式,調(diào)用winrar來進(jìn)行解壓。以下是遞歸解壓文件夾實(shí)例文章來源地址http://www.zghlxwxcb.cn/news/detail-745655.html
import os
import shutil
# 解壓縮rar到指定文件夾
# def extractRar(zfile, path, pwd):
# # rar_command1 = "C:\Program Files\WinRAR\WinRAR.exe x -ibck %s %s -p%s" % (zfile, path)
# # rar_command2 = r'"C:\Program Files\WinRAR\WinRAR.exe" x -ibck %s %s -p%s' % (zfile, path, pwd)
# os.system(rar_command2)
# extractRar('flag.rar', '', '-')
path = '.'
filename = 'flag.rar'
passwd = '-'
desdir = '.'
def unrarfile(path, zfile, passwd):
# 拼裝目標(biāo)文件
filePath = os.path.join(path, zfile)
# for fp in os.listdir(path):
# if fp[-3:] == 'rar':
# 執(zhí)行解壓命令
rar_command2 = r'"C:\Program Files\WinRAR\WinRAR.exe" x -ibck %s %s -p%s' % (filePath, desdir, passwd)
r = os.system(rar_command2)
print(r)
# 遞歸刪除解壓出來的文件夾
if path != '.':
print(path)
shutil.rmtree(path)
# 查詢最新解壓出來的文件夾,及其內(nèi)部文件
for fp2 in os.listdir(desdir):
if os.path.isdir(fp2):
# 文件夾名稱就是密碼
passwd = fp2
# print(passwd)
# 拼裝新的目標(biāo)文件夾
path = os.path.join(desdir,passwd)
# 讀取新的目標(biāo)文件
zfile = os.listdir(path)[0]
# 遞歸解壓
unrarfile(path,zfile,passwd)
unrarfile(path,filename, passwd)
到了這里,關(guān)于【Python腳本】使用Python解壓zip、rar文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!