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

cmd進入到python的Scripts目錄下運行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 : 需要壓縮的文件/文件夾名。從哪一級目錄開始,就會從哪一級開始壓縮;
output_file : 壓縮文件的輸出路徑及其壓縮的文件名;
可以是.rar, .zip;
root_path: input_file 所在目錄;
rar_path : WinRAR軟件的安裝路徑,
The default is 'C:/"Program Files"/WinRAR/WinRAR.exe'.
NOTE: 路徑和文件名中帶空格的時候一定要多加一重引號??!
"""
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)計目錄下,否則雖然腳本運行成功,但是不會生成解壓后的文件
附: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)
# 待壓縮的絕對路徑
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)建的壓縮對象用于執(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, "不是文件夾,不進行壓縮")
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文件,不進行解壓!")
continue
if __name__ == "__main__":
path = "G:/try/"
# Compress_path_zip(path)
Decompress_path_zip(path)總結
到此這篇關于python實現(xiàn)rar解壓和壓縮的文章就介紹到這了,更多相關python rar解壓和壓縮內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python實現(xiàn)對文件中圖片生成帶標簽的txt文件方法
下面小編就為大家分享一篇python實現(xiàn)對文件中圖片生成帶標簽的txt文件方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
手把手教你利用opencv實現(xiàn)人臉識別功能(附源碼+文檔)
最近搞一個人臉識別的項目練練手,不得不感嘆opencv做人臉檢測實在是強,這篇文章主要給大家介紹了關于利用opencv實現(xiàn)人臉識別功能的相關資料,并附上了源碼以及文檔,需要的朋友可以參考下2021-09-09

