日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

基于Python制作一個文件解壓縮工具_python

作者:Python?集中營 ? 更新時間: 2022-07-07 編程語言

經常由于各種壓縮格式的不一樣用到文件的解壓縮時就需要下載不同的解壓縮工具去處理不同的文件,以至于桌面上的壓縮工具就有三四種,于是使用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

欄目分類
最近更新