python實(shí)現(xiàn)rar解壓和壓縮的方法(附源碼)
1、前期準(zhǔn)備
python解壓rar格式文件,需要安裝rarfile模塊,去網(wǎng)上下載rarfile,我這里下載的是rarfile-2.4.tar.gz,解壓還需要對(duì)應(yīng)的UnRAR.exe
2、工具安裝
將rarfile-2.4.tar.gz拷貝到python安裝路徑的Scripts目錄下:

cmd進(jìn)入到python的Scripts目錄下運(yùn)行pip install rarfile-2.4.tar.gz,安裝成功如下圖:

3、解壓rar
import os
import rarfile
base_path = r'F:\python\rar'
def unrar(base_path):
files = os.listdir(base_path)
files.sort()
for path in files:
full_path = os.path.join(base_path, path)
print(full_path)
z = rarfile.RarFile(full_path)
z.extractall(base_path)
z.close()
os.remove(full_path)4、壓縮rar文件
def compress(input_file, output_file, root_path,
rar_path='D:/"Program Files"/WinRAR/WinRAR.exe'):
"""
調(diào)用CMD命令壓縮文件/文件夾
Parameters
----------
input_file : 需要壓縮的文件/文件夾名。從哪一級(jí)目錄開始,就會(huì)從哪一級(jí)開始?jí)嚎s;
output_file : 壓縮文件的輸出路徑及其壓縮的文件名;
可以是.rar, .zip;
root_path: input_file 所在目錄;
rar_path : WinRAR軟件的安裝路徑,
The default is 'C:/"Program Files"/WinRAR/WinRAR.exe'.
NOTE: 路徑和文件名中帶空格的時(shí)候一定要多加一重引號(hào)??!
"""
cmd_command = r'%s a %s %s' % (rar_path, output_file, input_file)
print(root_path)
os.chdir(root_path) # 切換工作目錄
print(cmd_command)
os.system(cmd_command)
if os.system(cmd_command)==0:
print('Successful backup to', output_file)
else:
print('Backup FAILED', input_file)
def rar(paths):
files = os.listdir(paths)
for path in files:
input_file = '"' + path + '"'
out = path.split('.')[0] + '_bak.rar'
out_file = '"' + out + '"'
print(path)
print(out)
compress(input_file,out_file,paths)def main():
unrar(base_path)
rar(base_path)
if __name__ == "__main__":
main()【注意】:
需要將UnRAR.exe文件放到和python腳本統(tǒng)計(jì)目錄下,否則雖然腳本運(yùn)行成功,但是不會(huì)生成解壓后的文件
附:Python批量壓縮解壓文件(zip、rar)
# -*- coding: utf-8 -*-
"""
@Time : 2023/8/28 14:47
@Auth : RS迷途小書童
@File :Compress and Decompress Folders.py
@IDE :PyCharm
@Purpose:批量壓縮/解壓文件夾
"""
import os
import zipfile
def Compress_path_zip(path_all):
path_all_list = os.listdir(path_all)
# 列出總文件夾內(nèi)所有需要壓縮的文件夾
for path_each in path_all_list:
path_compress = os.path.join(path_all, path_each)
# 待壓縮的絕對(duì)路徑
if os.path.isdir(path_compress):
print("正在壓縮:%s" % path_each)
# 遍歷所有需要壓縮的文件夾
if os.path.exists(path_all + "%s.zip" % path_each):
print(path_all + "%s1.zip" % path_each+"已存在,新文件名為'%s'" % (path_all + "%s1.zip" % path_each))
zip_file = zipfile.ZipFile(path_all + "%s1.zip" % path_each, 'w', zipfile.ZIP_DEFLATED, allowZip64=True)
# 創(chuàng)建的壓縮對(duì)象用于執(zhí)行后續(xù)操作
# file_list = zip_file.namelist()
# 列出壓縮文件夾中所有文件
else:
zip_file = zipfile.ZipFile(path_all + "%s.zip" % path_each, 'w', zipfile.ZIP_DEFLATED, allowZip64=True)
file_list = os.listdir(path_compress)
# 待壓縮文件夾內(nèi)所有文件
# zip_file.setpassword(b'123')
for file_each in file_list:
# 遍歷所有文件夾內(nèi)的文件
file_path = os.path.join(path_compress, file_each)
zip_file.write(file_path, file_each)
zip_file.close()
else:
print(path_each, "不是文件夾,不進(jìn)行壓縮")
continue
def Decompress_path_zip(path_all):
path_all_list = os.listdir(path_all)
for path_each in path_all_list:
# 遍歷所有需要壓縮的文件夾
if path_each.endswith('.zip'):
print("正在解壓:%s" % path_each)
path_decompress = os.path.join(path_all, path_each)
zip_file = zipfile.ZipFile(path_decompress, 'r') # 壓縮文件位置
for file in zip_file.namelist():
if os.path.exists(path_decompress[:-4]):
print("'%s'" % path_decompress[:-4], "已存在,新文件名為'%s'" % (path_decompress[:-4] + "1"))
zip_file.extract(file, path_decompress[:-4] + "1") # 解壓位置,pwd="1234".encode('utf-8')
else:
zip_file.extract(file, path_decompress[:-4]) # 解壓位置
zip_file.close()
else:
print(path_each, "非zip文件,不進(jìn)行解壓!")
continue
if __name__ == "__main__":
path = "G:/try/"
# Compress_path_zip(path)
Decompress_path_zip(path)總結(jié)
到此這篇關(guān)于python實(shí)現(xiàn)rar解壓和壓縮的文章就介紹到這了,更多相關(guān)python rar解壓和壓縮內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)對(duì)文件中圖片生成帶標(biāo)簽的txt文件方法
下面小編就為大家分享一篇python實(shí)現(xiàn)對(duì)文件中圖片生成帶標(biāo)簽的txt文件方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python爬蟲之場(chǎng)內(nèi)ETF基金獲取
這篇文章主要介紹了python爬蟲之場(chǎng)內(nèi)ETF基金獲取,ETF?是一種場(chǎng)內(nèi)交易型基金,可以在盤中進(jìn)行交易,交易性比場(chǎng)外基金強(qiáng)一點(diǎn),下文基于python的相關(guān)資料展開,需要的小伙伴可以參考一下2022-05-05
Django中URL的參數(shù)傳遞的實(shí)現(xiàn)
這篇文章主要介紹了Django中URL的參數(shù)傳遞的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python 實(shí)現(xiàn)將某一列設(shè)置為str類型
這篇文章主要介紹了Python 實(shí)現(xiàn)將某一列設(shè)置為str類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Python集成測(cè)試提高軟件質(zhì)量關(guān)鍵步驟探究
Python是一門強(qiáng)大的編程語言,提供了眾多工具和庫,用于執(zhí)行高效的集成測(cè)試,本文將深入介紹Python集成測(cè)試的概念、方法和最佳實(shí)踐,并通過豐富的示例代碼演示如何提高軟件質(zhì)量和減少潛在的缺陷2024-01-01
手把手教你利用opencv實(shí)現(xiàn)人臉識(shí)別功能(附源碼+文檔)
最近搞一個(gè)人臉識(shí)別的項(xiàng)目練練手,不得不感嘆opencv做人臉檢測(cè)實(shí)在是強(qiáng),這篇文章主要給大家介紹了關(guān)于利用opencv實(shí)現(xiàn)人臉識(shí)別功能的相關(guān)資料,并附上了源碼以及文檔,需要的朋友可以參考下2021-09-09
關(guān)于python中導(dǎo)入文件到list的問題
這篇文章主要介紹了關(guān)于python中導(dǎo)入文件到list的問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
通過實(shí)例簡(jiǎn)單了解Python sys.argv[]使用方法
這篇文章主要介紹了通過實(shí)例簡(jiǎn)單了解Python sys.argv[]使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08

