gpt4 book ai didi

Python:如何从 Google Drive 下载整个文件夹

转载 作者:行者123 更新时间:2023-12-04 01:51:48 28 4
gpt4 key购买 nike

我使用此代码从文件 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 中第一个参数的数字字符串是谷歌驱动器文件ID,通过右键单击文件并选择获取共享链接获得。

我尝试使用谷歌驱动器文件夹的共享链接 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

最佳答案

找到答案

!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)

关于Python:如何从 Google Drive 下载整个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52619878/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com