基于Python制作一個(gè)文件解壓縮工具
經(jīng)常由于各種壓縮格式的不一樣用到文件的解壓縮時(shí)就需要下載不同的解壓縮工具去處理不同的文件,以至于桌面上的壓縮工具就有三四種,于是使用python做了一個(gè)包含各種常見格式的文件解壓縮的小工具。

常見的壓縮格式主要是下面的四種格式:
zip 格式的壓縮文件,一般使用360壓縮軟件進(jìn)行解壓縮。
tar.gz 格式的壓縮文件,一般是在linux系統(tǒng)上面使用tar命令進(jìn)行解壓縮。
rar 格式的壓縮文件,一般使用rar壓縮軟件進(jìn)行解壓縮。
7z 格式的壓縮文件,一般使用7-zip壓縮軟件進(jìn)行解壓縮。
導(dǎo)入zip格式的解壓縮處理的非標(biāo)準(zhǔn)庫。
import os import zipfile as zip
編寫zip解壓縮格式的文件壓縮函數(shù)。
def do_zip(source_, target_file):
'''
zip文件壓縮
:param source_: 原始文件路徑
:param target_file: 目標(biāo)文件路徑
: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_: 目標(biāo)文件路徑
: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()
導(dǎo)入7z格式的解壓縮處理的非標(biāo)準(zhǔn)庫。
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_)
導(dǎo)入rar格式的解壓縮處理的非標(biāo)準(zhǔn)庫。
import rarfile as rar
編寫rar解壓縮格式的文件解壓縮函數(shù)。
def un_rar(source_file, target_):
'''
rar文件解壓縮
:param source_file: 原始文件
:param target_: 目標(biāo)文件路徑
:return:
'''
obj_ = rar.RarFile(source_file.decode('utf-8'))
obj_.extractall(target_.decode('utf-8'))
接下來開始進(jìn)入正題了,首先使用print函數(shù)打印一下菜單選項(xiàng),可以讓用戶在啟動(dòng)軟件后進(jìn)行菜單的選擇。
print('==========PYTHON工具:文件解壓縮軟件==========')
print('說明:目前支持zip、7z、rar格式')
print('1、文件解壓縮格式:zip/rar/7z')
print('2、文件操作類型(壓縮/解壓):Y/N')
print('3、文件路徑選擇,需要輸入相應(yīng)的操作文件路徑')
print('==========PYTHON工具:文件解壓縮軟件==========')
使用input函數(shù)接收用戶輸入的文件解壓縮格式。
format_ = input('請輸入文件解壓縮的格式:\n')
使用input函數(shù)接收用戶輸入的文件操作類型(壓縮/解壓)。
type_ = input('請輸入文件操作的類型:\n')
使用input函數(shù)接收用戶輸入的需要操作的文件路徑。
source_ = input('請輸入原始文件的存儲(chǔ)路徑(文件或目錄):\n')
使用input函數(shù)接收用戶輸入的生成的新文件的目標(biāo)路徑。
target_ = input('請輸入目標(biāo)文件的存儲(chǔ)路徑(文件或目錄):\n')
為了保持輸入的靈活性,加入不同格式不同操作類型的業(yè)務(wù)判斷。
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_)
目前功能點(diǎn)是做了三種格式,后期若是需要可能會(huì)擴(kuò)展升級(jí)當(dāng)前的版本。
到此這篇關(guān)于基于Python制作一個(gè)文件解壓縮工具的文章就介紹到這了,更多相關(guān)Python文件解壓縮內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)冒泡排序算法的完整實(shí)例
這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)冒泡排序算法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
淺談keras使用預(yù)訓(xùn)練模型vgg16分類,損失和準(zhǔn)確度不變
這篇文章主要介紹了淺談keras使用預(yù)訓(xùn)練模型vgg16分類,損失和準(zhǔn)確度不變,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編小編過來看看吧2020-07-07
Django Form and ModelForm的區(qū)別與使用
這篇文章主要介紹了Django Form and ModelForm的區(qū)別與使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
python游戲開發(fā)之視頻轉(zhuǎn)彩色字符動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了python游戲開發(fā)之視頻轉(zhuǎn)彩色字符動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04

