简体   繁体   中英

Download file google drive python

How do I download a file from googledrive ?

I am using pydrive using the link.

#https://drive.google.com/open?id=DWADADDSASWADSCDAW
    from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
drive = GoogleDrive(gauth)

gdrive_file = drive.CreateFile({'id': 'id=DWADADDSASWADSCDAW'})
gdrive_file.GetContentFile('DWADSDCXZCDWA.zip') # Download content file.

Error:

raceback (most recent call last):
  File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\oauth2client\clientsecrets.py", line 121, in _loadfile
    with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 386, in LoadClientConfigFile
    client_type, client_info = clientsecrets.loadfile(client_config_file)
  File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\oauth2client\clientsecrets.py", line 165, in loadfile
    return _loadfile(filename)
  File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\oauth2client\clientsecrets.py", line 125, in _loadfile
    exc.strerror, exc.errno)
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "C:/Users/Hoxton/123/pyu_test.py", line 8, in <module>
        gdrive_file.GetContentFile('PyUpdater+App-win-1.0.zip') # Download content file.
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\files.py", line 210, in GetContentFile
        self.FetchContent(mimetype, remove_bom)
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\files.py", line 42, in _decorated
        self.FetchMetadata()
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 57, in _decorated
        self.auth.LocalWebserverAuth()
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 113, in _decorated
        self.GetFlow()
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 443, in GetFlow
        self.LoadClientConfig()
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 366, in LoadClientConfig
        self.LoadClientConfigFile()
      File "C:\Users\Hoxton\AppData\Local\Continuum\miniconda3\lib\site-packages\pydrive\auth.py", line 388, in LoadClientConfigFile
        raise InvalidConfigError('Invalid client secrets file %s' % error)
    pydrive.settings.InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

    Process finished with exit code 1

Try the provided sample code in the documentation .

The Drive API allows you to download files that are stored in Google Drive. Also, you can download exported versions of Google Documents (Documents, Spreadsheets, Presentations, etc.) in formats that your app can handle. Drive also supports providing users direct access to a file via the URL in the webViewLink property.

Here is the code snippet :

file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print "Download %d%%." % int(status.progress() * 100)

This works for me:

from google_drive_downloader import GoogleDriveDownloader as gdd

gdd.download_file_from_google_drive(file_id='1z8e2CnvrX8ZSu2kk0QgiFWurOKMr0', dest_path='E:/model.h5')

Source: https://newbedev.com/python-download-files-from-google-drive-using-url

Hey I know it's a bit late to answer, but it might still be helpful to someone.

I had a similar problem with G-sheets, the problem here is that there might be multiple formats to download the file in and you're not specifying which one you want.To do this you need to add the mimetype parameter to the GetContentFile Method. Like so:

gdrive_file.GetContentFile('DWADSDCXZCDWA.zip', mimetype = 'application/zip')

Note that there are multiple mimetypes for zip files and that the mimetype and extension need to agree. So you need to know which one to use, or just try out different ones if you don't. Here's a handy list:

  • application/x-compressed
  • application/x-zip-compressed
  • application/zip
  • multipart/x-zip

Furthermore, if you actually access the metadata of the file you can have a peek at all the types of formats you can export it in under 'exportLinks'. There will be a dict with mimetypes and the associated links.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM