Python:如何从Google云端硬盘下载整个文件夹

时间:2018-10-03 04:49:58

标签: python

我使用此代码从文件ID下载Google驱动器文件,并为其提供一个文件名

!pip install -U -q PyDrive

from google.colab import files
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
import os
import sys


auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

def downloadFiles(googleID, fileName):
    print("Downloading ", fileName)
    toDl = drive.CreateFile({'id': googleID})
    toDl.GetContentFile(fileName)

downloadFiles("0B7Mdz82xyAxcMzJYOFhfg5gfMnp6Tl9Cei00U3BKTGNN","news.npy")

downloadFiles中第一个参数的数字字符串是google驱动器文件ID,可通过右键单击文件并选择“获取共享”链接获得。

我尝试使用Google驱动器文件夹的共享链接ID进行操作,这是我得到的错误

downloadFiles("1kdNDfvg2zyyXYYrcXVnuuhIOnSLgtrnE","dlFolder")

---------------------------------------------------------------------------
FileNotDownloadableError                  Traceback (most recent call last)
<ipython-input-4-b59b205fcc37> in <module>()
      1 
      2 
----> 3 downloadFiles("1kdNDfvg2zyyXYYrcXVnuuhIOnSLgtrnE","news")

<ipython-input-2-cbfb11eb1c03> in downloadFiles(googleID, fileName)
      2     print("Downloading ", fileName)
      3     toDl = drive.CreateFile({'id': googleID})
----> 4     toDl.GetContentFile(fileName)

/usr/local/lib/python3.6/dist-packages/pydrive/files.py in GetContentFile(self, filename, mimetype, remove_bom)
    208                     type(self.content) is not io.BytesIO or \
    209                     self.has_bom == remove_bom:
--> 210       self.FetchContent(mimetype, remove_bom)
    211     f = open(filename, 'wb')
    212     f.write(self.content.getvalue())

/usr/local/lib/python3.6/dist-packages/pydrive/files.py in _decorated(self, *args, **kwargs)
     41     if not self.uploaded:
     42       self.FetchMetadata()
---> 43     return decoratee(self, *args, **kwargs)
     44   return _decorated
     45 

/usr/local/lib/python3.6/dist-packages/pydrive/files.py in FetchContent(self, mimetype, remove_bom)
    263     else:
    264       raise FileNotDownloadableError(
--> 265         'No downloadLink/exportLinks for mimetype found in metadata')
    266 
    267     if mimetype == 'text/plain' and remove_bom:

FileNotDownloadableError: No downloadLink/exportLinks for mimetype found in metadata

1 个答案:

答案 0 :(得分:1)

找到答案

!pip install -U -q PyDrive
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# choose a local (colab) directory to store the data.
local_download_path = ''
try:
    os.makedirs(local_download_path)
except: pass

# 2. Auto-iterate using the query syntax
#    https://developers.google.com/drive/v2/web/search-parameters
file_list = drive.ListFile(
    {'q': "'1m4t_k5N7-W3saafdfddfdWCqfc1D0xHmc20r' in parents"}).GetList()  #use your own folder ID here

for f in file_list:
    # 3. Create & download by id.
    print('title: %s, id: %s' % (f['title'], f['id']))
    fname = f['title']
    print('downloading to {}'.format(fname))
    f_ = drive.CreateFile({'id': f['id']})
    f_.GetContentFile(fname)