import glob
import os
import gzip
import tarfile
import zipfile
import rarfile
import time
path = r'/root/projects/TransT/data/TrackingNet/TRAIN_0/TRAIN_0/zips'
file_lst = glob.glob(path + '/*')
filename_lst = [os.path.basename(i) for i in file_lst]
# print(filename_lst)
def unzipf(name):
zip_file = zipfile.ZipFile(name)
# 类似tar解除打包,建立文件夹存放解压的多个文件
if not os.path.isdir(name):
filename = os.path.splitext(name)[0]
os.mkdir(filename)
print(filename)
for names in zip_file.namelist():
zip_file.extract(names, filename)
zip_file.close()
for name in file_lst:
if '.' in name:
suffix = name.split('.')[-1]
if suffix == 'zip':
filename = os.path.basename(name)
filename_1 = filename.split('.')[0]
# print(filename_1)
if filename_1 not in filename_lst:
print(filename)
# unzipf(name)
# def ungz(filename):
# filename = filename[:-3]
# gz_file = gzip.GzipFile(filename)
# with open(filename, "w+") as file:
# file.write(gz_file.read())
# return filename
# def untar(filename):
# tar = tarfile.open(filename)
# names = tar.getnames()
# if not os.path.isdir(filename + "_dir"):
# os.mkdir(filename + "_dir")
# for name in names:
# tar.extract(name, filename + "_dir/")
# tar.close()
# def unzip(file):
# zip_file = zipfile.ZipFile(file)
# if not os.path.isdir(file):
# filename = os.path.basename(file)
# os.mkdir(filename)
# for names in zip_file.namelist():
# zip_file.extract(names,filename)
# zip_file.close()
def unzip(file):
zip_file = zipfile.ZipFile(file)
# 类似tar解除打包,建立文件夹存放解压的多个文件
if not os.path.isdir(file):
filename = os.path.splitext(file)[0]
print(filename)
os.mkdir(filename)
for names in zip_file.namelist():
zip_file.extract(names, filename)
zip_file.close()
# def unrar(filename):
# rar = rarfile.RarFile(filename)
# if not os.path.isdir(filename + "_dir"):
# os.mkdir(filename + "_dir")
# os.chdir(filename + "_dir")
# rar.extractall()
# rar.close()
def unzip_files():
for file in file_lst:
if '.' in file:
suffix = file.split('.')[-1]
# if suffix == 'gz':
# new_filename = ungz(filename)
# os.remove(filename)
# if new_filename.split('.')[-1] == 'tar':
# untar(new_filename)
# os.remove(new_filename)
# if suffix == 'tar':
# untar(filename)
# os.remove(filename)
if suffix == 'zip':
unzip(file)
# if suffix == 'rar':
# unrar(filename)
# os.remove(filename)
# unzip_files()
批量解压到一半时中断 重新进行批量解压 选择未解压的进行解压
最新推荐文章于 2023-05-05 14:00:00 发布