前言
使用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")