網站首頁 編程語言 正文
經常由于各種壓縮格式的不一樣用到文件的解壓縮時就需要下載不同的解壓縮工具去處理不同的文件,以至于桌面上的壓縮工具就有三四種,于是使用python做了一個包含各種常見格式的文件解壓縮的小工具。
常見的壓縮格式主要是下面的四種格式:
zip 格式的壓縮文件,一般使用360壓縮軟件進行解壓縮。
tar.gz 格式的壓縮文件,一般是在linux系統上面使用tar命令進行解壓縮。
rar 格式的壓縮文件,一般使用rar壓縮軟件進行解壓縮。
7z 格式的壓縮文件,一般使用7-zip壓縮軟件進行解壓縮。
導入zip格式的解壓縮處理的非標準庫。
import os
import zipfile as zip
編寫zip解壓縮格式的文件壓縮函數。
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解壓縮格式的文件解壓縮函數。
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解壓縮格式的文件壓縮函數。
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解壓縮格式的文件解壓縮函數。
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解壓縮格式的文件解壓縮函數。
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函數打印一下菜單選項,可以讓用戶在啟動軟件后進行菜單的選擇。
print('==========PYTHON工具:文件解壓縮軟件==========')
print('說明:目前支持zip、7z、rar格式')
print('1、文件解壓縮格式:zip/rar/7z')
print('2、文件操作類型(壓縮/解壓):Y/N')
print('3、文件路徑選擇,需要輸入相應的操作文件路徑')
print('==========PYTHON工具:文件解壓縮軟件==========')
使用input函數接收用戶輸入的文件解壓縮格式。
format_ = input('請輸入文件解壓縮的格式:\n')
使用input函數接收用戶輸入的文件操作類型(壓縮/解壓)。
type_ = input('請輸入文件操作的類型:\n')
使用input函數接收用戶輸入的需要操作的文件路徑。
source_ = input('請輸入原始文件的存儲路徑(文件或目錄):\n')
使用input函數接收用戶輸入的生成的新文件的目標路徑。
target_ = input('請輸入目標文件的存儲路徑(文件或目錄):\n')
為了保持輸入的靈活性,加入不同格式不同操作類型的業務判斷。
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_)
目前功能點是做了三種格式,后期若是需要可能會擴展升級當前的版本。
原文鏈接:https://www.cnblogs.com/lwsbc/p/16264174.html
相關推薦
- 2022-07-22 jQuery實現點擊顯示密碼框密碼
- 2022-12-09 重學c/c++之數據存儲詳解(整數、浮點數)_C 語言
- 2022-07-07 Python推導式使用詳情_python
- 2022-08-23 python畫圖中文不顯示問題的解決方法_python
- 2022-04-03 ?Python?代碼制作動態鞭炮_python
- 2023-10-15 小程序動態隱藏分享按鈕
- 2022-10-23 React日期時間顯示組件的封裝方法_React
- 2022-12-21 詳解Golang中Channel的原理和使用技巧_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支