[GEE Tips-2]自行打包(压缩)Google Drive中的文件夹

前言

使用GEE生成多个文件到Google Drive之后,如果想一次下载下来,它就会对这些文件自动进行压缩打包,但是这个过程往往进行的很慢,经常还会莫名其妙的崩掉。
解决的方法也是有的,就是用Colab的Python对Google Drive中的文件进行打包,而且绝不会崩掉。

1.挂载Gooogle Drive

from google.colab import drive 
drive.mount('/gdrive') 

2.压缩

import os, zipfile

def make_zip(source_dir, output_filename):
    zipf = zipfile.ZipFile(output_filename, 'w')    
    pre_len = len(os.path.dirname(source_dir))
    for parent, dirnames, filenames in os.walk(source_dir):
        for filename in filenames:
            pathfile = os.path.join(parent, filename)
            arcname = pathfile[pre_len:].strip(os.path.sep)     #相对路径
            zipf.write(pathfile, arcname)
    zipf.close()
    
dir = "/gdrive/My Drive/DirToCompress"       #指定要压缩的文件夹
zipFile = "/gdrive/My Drive/Compressed.zip"  #指定压缩后的文件
make_zip(dir,zipFile)
print("Success")
原文地址:https://www.cnblogs.com/wszhang/p/12219399.html