解压嵌套压缩文件

本文介绍了一个Python程序,使用tkinter库实现文件选择功能,通过shutil模块进行解压操作,并处理可能出现的同名问题。主要函数包括openFile()获取文件路径,unpack_entry()调用解压函数。
摘要由CSDN通过智能技术生成
import shutil
import os
from tkinter import filedialog as tf
import tkinter as tk
from tkinter import ttk

def unpack(file: str):
    dir_name = os.path.dirname(file)
    extract_dir = os.path.join(dir_name, os.path.basename(file).split('.')[0])
    # 解决同名问题
    if os.path.exists(extract_dir):
        temp = input(f"{extract_dir} 已经存在,请换一个名字:\n")
        extract_dir = os.path.join(dir_name, temp)
    shutil.unpack_archive(file, extract_dir)
    pack_list = []
    for root, dirs, files in os.walk(extract_dir):
        for file_name in files:
            if file_name.endswith((".zip")):
                pack_list.append(os.path.join(root, file_name))
    for p in pack_list:
        unpack(p)
                

def openFile():
    filepath = tf.askopenfilename()
    return filepath

def unpack_entry():
    file_path = openFile()
    unpack(file_path)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值