棒棒哒Bruce:1044890889@qq.com
python解压多个文件夹下的压缩文件,以bz2压缩文件为例
以colorferet数据集为例,里面的图片都是压缩文件,对上百个文件夹里的数据进行手动解压想想都可怕。以下是该数据集的文件示例。
处理数据时,经常遇到大量压缩文件,手动处理非常低效
使用python3.6自动解压该数据集下的所有文件,同时解压后还可以删除压缩文件
解压文件代码示例
# -*- coding: utf-8 -*-
import os
import bz2
base_path = r'E:\colorferet\colorferet\dvd1\data\images'
files = os.listdir(base_path)
files.sort(key=lambda x: int(x.split('.')[0]))
for path in files:
full_path = os.path.join(base_path, path)
#print(full_path)
path = str(full_path)
un_path = str(full_path)
for (dirpath, dirnames, files1) in os.walk(path):
for filename in files1:
filepath = os.path.join(dirpath, filename)
newfilepath = os.path.join(un_path, filename + '.ppm')
with open(newfilepath, 'wb') as new_file, bz2.BZ2File(filepath, 'rb') as file:
for data in iter(lambda: file.read(100 * 1024), b''):
new_file.write(data)
删除压缩文件
# -*- coding: utf-8 -*-
import os
base_path = r'E:\colorferet\colorferet\dvd1\data\images'
files = os.listdir(base_path)
files.sort(key=lambda x: int(x.split('.')[0]))
for path in files:
full_path = os.path.join(base_path, path)
#print(full_path)
path = str(full_path)
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith(".bz2"): # 指定要删除的格式,这里是bz2压缩文件 可以换成其他格式
os.remove(os.path.join(root, name))
print("Delete File: " + os.path.join(root, name))
亲测有效哦,希望可以帮到大家
这个是解压并删除压缩文件后的效果