import os import tarfile import zipfile def un_landsat8_file(path,out): classs1 = os.listdir(path) for idx, folder in enumerate(classs1): if folder[-7:]==".tar.gz": f_name = folder.replace(".tar.gz", "") print(f_name) outfile = out + "\\" + f_name tar = tarfile.open(path + "\\" + folder) # 打开影像压缩包 names = tar.getnames() # 获取影像压缩包里包含的文件名 for name in names: tar.extract(name, path=outfile) # 开始解压(这里解压的是所有文件,也可以只解压其中的某一个文件,指定文件名就好) tar.close() print(f_name,"提取完成") def un_zip_sentinel(file_name,upload_path): """unzip zip file""" zip_file = zipfile.ZipFile(os.path.join(upload_path,file_name)) file_name_pre=os.path.basename(file_name).split(‘.‘)[0] restore_path=os.path.join(upload_path,file_name_pre) print(restore_path) # if not os.path.exists(restore_path): # os.makedirs(restore_path) # for names in zip_file.namelist(): # zip_file.extract(names,restore_path) # zip_file.close() return restore_path if __name__ == ‘__main__‘: # 网上下的Landsat数据都是压缩包(.tar.gz),因此需调用tarfile包(其他类似的解压缩包也可以) path =r‘F:\algorithm\解压缩\1‘ out=r‘F:\algorithm\解压缩\成果‘ classs1 = os.listdir(path) for idx, folder in enumerate(classs1): print(folder) sentinel_path=os.path.join(path,folder) un_zip_sentinel(sentinel_path, out)