前言
工作中经常处理工单日志,每次都要手动解压,非常不方便,因此写了一个脚本,用于一键解压。
一、环境
python 3.8.0
三方库:
import zipfile
import py7zr
import rarfile
import tarfile
二、源码
如下,直接贴出代码和注释
import os
import zipfile
import py7zr
import rarfile
import tarfile
unziperr_flag = 0
def unzip_path(dirpath,filepath):
global unziperr_flag
ret = 0
if zipfile.is_zipfile(filepath):
with zipfile.ZipFile(filepath, 'r') as f:
ret = 1
try:
f.extractall(dirpath)
except:
unziperr_flag = 0
print("zip fail!", filepath)
elif py7zr.is_7zfile(filepath):
with py7zr.SevenZipFile(filepath, 'r') as f:
ret = 1
try:
f.extractall(dirpath)
except:
unziperr_flag = 0
print("7z fail!", filepath)
elif rarfile.is_rarfile(filepath):
with rarfile.RarFile(filepath, 'r') as f:
ret = 1
try:
f.extractall(dirpath)
except:
unziperr_flag = 0
print("rar fail!", filepath)
elif tarfile.is_tarfile(filepath):
with tarfile.open(filepath, 'r') as f:
ret = 1
try:
f.extractall(dirpath)
except:
print("tar fail!", filepath)
if ret:
os.remove(filepath)
#print("ret = ", ret, filepath)
return ret
def unzip_file(root_path):
if zipfile.is_zipfile(root_path):
with zipfile.ZipFile(root_path, 'r') as zf:
dirname = os.path.splitext(root_path)[0].rsplit(".", 1)[0] # 获取压缩文件名
try:
os.makedirs(dirname)
except:
print("\n")
zf.extractall(dirname)
elif py7zr.is_7zfile(root_path):
with py7zr.SevenZipFile(root_path, 'r') as szf:
dirname = os.path.splitext(root_path)[0].rsplit(".", 1)[0] # 获取压缩文件名
try:
os.makedirs(dirname)
except:
print("\n")
szf.extractall(dirname)
elif rarfile.is_rarfile(root_path):
with rarfile.RarFile(root_path, 'r') as rf:
dirname = os.path.splitext(root_path)[0].rsplit(".", 1)[0] # 获取压缩文件名
try:
os.makedirs(dirname)
except:
print("\n")
rf.extractall(dirname)
elif tarfile.is_tarfile(root_path):
with tarfile.open(root_path, 'r') as tf:
dirname = os.path.splitext(root_path)[0].rsplit(".", 1)[0] # 获取压缩文件名
try:
os.makedirs(dirname)
except:
print("\n")
tf.extractall(dirname)
elif root_path.endswith('.gz'):
with gzip.open(root_path, 'rb') as gz:
with open(os.path.splitext(root_path)[0], 'wb') as f:
f.write(gz.read())
return 1
def extract_all_archive_files(root_dir):
ret = 0
# 判断是路径还是文件
if os.path.isdir(root_dir):
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
ret += unzip_path(dirpath, filepath)
elif os.path.isfile(root_dir):
ret += unzip_file(root_dir)
root_dir = root_dir.rsplit(".", 1)[0]
else:
print("输入的路径不是有效的文件夹或者压缩文件")
return ret, root_dir
if __name__ == '__main__':
print(">-------------------------------------------------------------------------------------------------<")
print("---author: lxjswd--------------------------------------------------")
print("---complete date: 2023-6-17------------------------------------------------")
print("---last change date: 2023-9-8-------------------------------------------------")
print("---note: you could decompression zip、7z 、rar、tar、gz files------")
print("---9.8更新说明:增加解压失败异常处理------------------------------------------------")
print(">-------------------------------------------------------------------------------------------------<\n\n")
while 1:
read_path = input("please input unzip path(输入q退出):")
if read_path == 'q':
print(">-----------------------------------------<")
print(">----------------good bye-----------------<")
print(">-----------------------------------------<\n\n\n\n\n")
break
print("read_path : ", read_path)
flag = 1
num = 0
unziperr_flag = 1
while 1:
num += 1
print("unzip times ", num)
flag, read_path = extract_all_archive_files(read_path)
if flag == 0 & unziperr_flag == 1:
print(">-------------------------------------------------------------------------------------------------<")
print("unzip succesful!")
break
elif flag == 0 & unziperr_flag == 0:
print(">!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<")
print("----------------------------------error !!! something unzip err!-----------------------------------\n\n")
break
三、注意事项
在运行脚本后,可以输入单个压缩文件路径如D:\Desk\test\test.zip
,也可以输入直接压缩文件所在文件夹路径,如D:\Desk\test