解压文件,支持zip rar gz格式
工具类直接传文件路径即可
自动根据文件名选择解压方式。
可能解压rar会报错,https://www.bbsmax.com/A/1O5Er1W8d7/
解决连接,缺少的库。不需要的直接注释就好。有用,可以点个赞。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
"""
@author: zhengxianjun
@contact: 1596492090@qq.com
@datetime:2020/7/13 下午2:31
@software: PyCharm
"""
import os
import tarfile
from unrar import rarfile
import zipfile
class FileUtils:
@staticmethod
def is_exist(path):
path0 = os.path.splitext(path)[0] # 分离扩展名
if not os.path.isdir(path0):
os.makedirs(path0)
return path0
@staticmethod
def un_tgz(src_path:str,dist_path=None):
"""
解压tgz压缩文件
"""
tar = tarfile.open(src_path)
if not dist_path: ##默认解压到当前同级压缩包目录下
# 判断同名文件夹是否存在,若不存在则创建同名文件夹
path = FileUtils.is_exist(src_path)
el