I use this code to download Google drive files from the file id, and give a file it's own name
!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")
Where the string of numbers for the first argument in downloadFiles
is the google drive file ID, which is obtained by right clicking the file and selecting get share link.
I tried doing with with a share link ID for a google drive folder, this is the error I got
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