第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

怎么用python做一個(gè)解壓縮小工具,以后再也不用下載各種格式的解壓縮軟件了...

標(biāo)簽:
Python

经常由于各种压缩格式的不一样用到文件的解压缩时就需要下载不同的解压缩工具去处理不同的文件,以至于桌面上的压缩工具就有三四种,于是使用python做了一个包含各种常见格式的文件解压缩的小工具。

file

常见的压缩格式主要是下面的四种格式:

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_)

目前功能点是做了三种格式,后期若是需要可能会扩展升级当前的版本。欢迎大家在评论区留言,提供比较新的思路~

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消