經(jīng)常由于各種壓縮格式的不一樣用到文件的解壓縮時就需要下載不同的解壓縮工具去處理不同的文件,以至于桌面上的壓縮工具就有三四種,于是使用python做了一個包含各種常見格式的文件解壓縮的小工具。
你所需要的網(wǎng)站建設服務,我們均能行業(yè)靠前的水平為你提供.標準是產(chǎn)品質(zhì)量的保證,主要從事成都做網(wǎng)站、網(wǎng)站設計、企業(yè)網(wǎng)站建設、成都做手機網(wǎng)站、網(wǎng)頁設計、高端網(wǎng)站設計、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。成都創(chuàng)新互聯(lián)擁有實力堅強的技術研發(fā)團隊及素養(yǎng)的視覺設計專才。
閱讀全文
常見的壓縮格式主要是下面的四種格式:
zip 格式的壓縮文件,一般使用360壓縮軟件進行解壓縮。
tar.gz 格式的壓縮文件,一般是在linux系統(tǒng)上面使用tar命令進行解壓縮。
rar 格式的壓縮文件,一般使用rar壓縮軟件進行解壓縮。
7z 格式的壓縮文件,一般使用7-zip壓縮軟件進行解壓縮。
導入zip格式的解壓縮處理的非標準庫。
import os
import zipfile as zip
編寫zip解壓縮格式的文件壓縮函數(shù)。
def do_zip(source_, target_file):
'''
zip文件壓縮
:param source_: 原始文件路徑
:param target_file: 目標文件路徑
:return:
'''
zip_file = zip.ZipFile(target_file, 'w')
pre_len = len(os.path.dirname(source_))
for parent, dirnames, filenames in os.walk(source_):
for filename in filenames:
print(f'{filename}')
path_file = os.path.join(parent, filename)
arcname = path_file[pre_len:].strip(os.path.sep)
zip_file.write(path_file, arcname)
zip_file.close()
編寫zip解壓縮格式的文件解壓縮函數(shù)。
def un_zip(source_file, target_):
'''
zip文件解壓縮
:param source_file: 原始文件路徑
:param target_: 目標文件路徑
:return:
'''
zip_file = zip.ZipFile(source_file)
if os.path.isdir(target_):
pass
else:
os.mkdir(target_)
for names in zip_file.namelist():
zip_file.extract(names, target_)
zip_file.close()
導入7z格式的解壓縮處理的非標準庫。
import py7zr
編寫7z解壓縮格式的文件壓縮函數(shù)。
def do_7z(source_, target_file):
'''
7z文件壓縮
:param source_:
:param target_file:
:return:
'''
with py7zr.SevenZipFile(target_file, 'r') as file:
file.extractall(path=source_)
編寫7z解壓縮格式的文件解壓縮函數(shù)。
def un_7z(source_file, target_):
'''
7z文件解壓縮
:param source_file:
:param target_:
:return:
'''
with py7zr.SevenZipFile(source_file, 'w') as file:
file.writeall(target_)
導入rar格式的解壓縮處理的非標準庫。
import rarfile as rar
編寫rar解壓縮格式的文件解壓縮函數(shù)。
def un_rar(source_file, target_):
'''
rar文件解壓縮
:param source_file: 原始文件
:param target_: 目標文件路徑
:return:
'''
obj_ = rar.RarFile(source_file.decode('utf-8'))
obj_.extractall(target_.decode('utf-8'))
接下來開始進入正題了,首先使用print函數(shù)打印一下菜單選項,可以讓用戶在啟動軟件后進行菜單的選擇。
print('==========PYTHON工具:文件解壓縮軟件==========')
print('說明:目前支持zip、7z、rar格式')
print('1、文件解壓縮格式:zip/rar/7z')
print('2、文件操作類型(壓縮/解壓):Y/N')
print('3、文件路徑選擇,需要輸入相應的操作文件路徑')
print('==========PYTHON工具:文件解壓縮軟件==========')
使用input函數(shù)接收用戶輸入的文件解壓縮格式。
format_ = input('請輸入文件解壓縮的格式:\n')
使用input函數(shù)接收用戶輸入的文件操作類型(壓縮/解壓)。
type_ = input('請輸入文件操作的類型:\n')
使用input函數(shù)接收用戶輸入的需要操作的文件路徑。
source_ = input('請輸入原始文件的存儲路徑(文件或目錄):\n')
使用input函數(shù)接收用戶輸入的生成的新文件的目標路徑。
target_ = input('請輸入目標文件的存儲路徑(文件或目錄):\n')
為了保持輸入的靈活性,加入不同格式不同操作類型的業(yè)務判斷。
if format_ == 'zip' and type_ == 'Y':
do_zip(source_, target_)
elif format_ == 'zip' and type_ == 'N':
un_zip(source_, target_)
elif format_ == 'rar' and type_ == 'Y':
un_zip(source_, target_)
elif format_ == 'rar' and type_ == 'N':
un_zip(source_, target_)
elif format_ == '7z' and type_ == 'Y':
un_zip(source_, target_)
elif format_ == '7z' and type_ == 'N':
un_zip(source_, target_)
目前功能點是做了三種格式,后期若是需要可能會擴展升級當前的版本。歡迎大家在評論區(qū)留言,提供比較新的思路~
【往期精彩】
python中最簡單的turtle繪圖:奧運五環(huán)!
知識匯總:python辦公自動化應該學習哪些內(nèi)容?
python多線程同步售票系統(tǒng)解決思路...
發(fā)現(xiàn)幾個好玩的游戲編程平臺,與君共勉!
python四個性能檢測工具,包括函數(shù)的運行內(nèi)存、時間等等...
文章名稱:怎么用python做一個解壓縮小工具,以后再也不用下載各種格式的解壓縮軟件了...
文章URL:http://chinadenli.net/article8/dsogpop.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站設計公司、軟件開發(fā)、服務器托管、靜態(tài)網(wǎng)站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)