import threading import time import os import tarfile input_path = r"D:\jieyaqian" out = r'D:\jieyahou' classs = os.listdir(input_path) def repress(folder, input_path, out): print(1, folder) ori_tif = os.path.join(input_path, folder) print(ori_tif) name = os.path.basename(ori_tif) print(name) # 解压单个文件 if not os.path.exists(os.path.join(out, name)): os.makedirs(os.path.join(out, name)) tf = tarfile.open(ori_tif) tf.extractall(os.path.join(out, name)) def single(): print('begin-------------') for folder in classs: repress(folder, input_path, out) print('end-------------') def multiple(): print('begin-------------') threads = [] for folder1 in classs: threads.append( threading.Thread(target=repress(folder1, input_path, out)) ) for thread in threads: thread.start() for thread in threads: thread.join() print('end-------------') if __name__ == '__main__': start = time.time() multiple() end = time.time() print('single thread cost:', end - start, 'seconds')
批量解压zip文件
import os import zipfile import pathlib def unzip_file(zip_src, dst_dir): r = zipfile.is_zipfile(zip_src) if r: fz = zipfile.ZipFile(zip_src, 'r') for file in fz.namelist(): if pathlib.Path(file).suffix == '.zip': print(os.path.join(dst_dir, file), os.path.join(dst_dir, pathlib.Path(file).parent)) unzip_file(os.path.join(dst_dir, file), os.path.join(dst_dir, pathlib.Path(file).parent)) fz.extract(file, dst_dir) else: print('This is not zip') unzip_file(r"D:\test\加压数据\源数据\VOCdevkit.zip", r"D:\test\加压数据\解压后")
标签:解压,join,批量,file,import,print,path,多线程,os From: https://www.cnblogs.com/suoyike1001/p/17353312.html