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)
解压嵌套压缩文件
最新推荐文章于 2024-03-18 09:53:49 发布
本文介绍了一个Python程序,使用tkinter库实现文件选择功能,通过shutil模块进行解压操作,并处理可能出现的同名问题。主要函数包括openFile()获取文件路径,unpack_entry()调用解压函数。
摘要由CSDN通过智能技术生成