【问题标题】:file download from google drive to colaboratory从谷歌驱动器下载文件到 colaboratory
【发布时间】:2018-07-21 23:32:48
【问题描述】:

我正在尝试将文件从我的谷歌驱动器下载到 colaboratory。

file_id = '1uBtlaggVyWshwcyP6kEI-y_W3P8D26sz'

import io
from googleapiclient.http import MediaIoBaseDownload

request = drive_service.files().get_media(fileId=file_id)
downloaded = io.BytesIO()
downloader = MediaIoBaseDownload(downloaded, request)
done = False
while done is False:
  # _ is a placeholder for a progress object that we ignore.
  # (Our file is small, so we skip reporting progress.)
  _, done = downloader.next_chunk()

downloaded.seek(0)
print('Downloaded file contents are: {}'.format(downloaded.read()))

这样做会出现此错误:

NameError: name 'drive_service' is not defined

如何消除这个错误?

【问题讨论】:

    标签: google-colaboratory


    【解决方案1】:

    您需要定义一个驱动API服务客户端来与谷歌驱动API交互,例如:

    from googleapiclient.discovery import build
    drive_service = build('drive', 'v3')
    

    (见笔记本External data: Drive, Sheets, and Cloud Storage/Drive REST API

    【讨论】:

      【解决方案2】:

      我建议您使用 Pydrive 从 google drive 下载您的文件。我下载 500MB 数据集 5s。 1.安装Pydrive

      !pip install PyDrive
      

      2。 OAout

      from pydrive.auth import GoogleAuth
      from pydrive.drive import GoogleDrive
      
      gauth = GoogleAuth()
      gauth.LocalWebserverAuth()
      
      drive = GoogleDrive(gauth)
      

      3。从谷歌驱动器下载文件的代码

      fileId = drive.CreateFile({'id': 'DRIVE_FILE_ID'}) #DRIVE_FILE_ID is file id example: 1iytA1n2z4go3uVCwE_vIKouTKyIDjEq
      print fileId['title']  # UMNIST.zip
      fileId.GetContentFile('UMNIST.zip')  # Save Drive file as a local file
      

      为莫圣战加油

      【讨论】:

        【解决方案3】:

        第 1 步

        !pip install -U -q PyDrive
        

        第 2 步

        from pydrive.auth import GoogleAuth
        from pydrive.drive import GoogleDrive
        from google.colab import auth
        from oauth2client.client import GoogleCredentials
        # Authenticate and create the PyDrive client.
        # This only needs to be done once per notebook.
        auth.authenticate_user()
        gauth = GoogleAuth()
        gauth.credentials = GoogleCredentials.get_application_default()
        drive = GoogleDrive(gauth)
        

        第三步

        file_id = '17Cp4ZxCYGzWNypZo1WPiIz20x06xgPAt' # URL id. 
        downloaded = drive.CreateFile({'id': file_id})
        downloaded.GetContentFile('shaurya.txt')
        

        第四步

        !ls #to verify content
        

        import os
        print(os.listdir())
        

        【讨论】:

        • 特别是对于有限的共享文件,这个效果很好。
        【解决方案4】:

        您还可以在https://github.com/ruelj2/Google_drive 上使用我在 google.colab 和 PyDrive 上的实现,这样会更容易。

        !pip install - U - q PyDrive  
        import os  
        os.chdir('/content/')  
        !git clone https://github.com/ruelj2/Google_drive.git  
        
        from Google_drive.handle import Google_drive  
        Gd = Google_drive()  
        Gd.load_file(local_dir, file_ID)
        

        【讨论】:

          【解决方案5】:

          将文件从谷歌驱动器下载到 colabo 笔记本的最简单方法是通过 colabo api:

          from google.colab import drive
          drive.mount('/content/gdrive')
          
          !cp '/content/gdrive/My Drive/<file_path_on_google_drive>' <filename_in_colabo>
          

          备注:

          1. 通过 drive.mount(),您可以访问 google 驱动器上的任何文件。
          2. “我的云端硬盘”相当于您本地文件系统上的“Google 云端硬盘”。
          3. file_path 用单引号括起来,因为挂载点下方的标准目录(“我的驱动器”)有一个空格,而且您的路径中的其他地方也可能有空格。
          4. 文件浏览器(通过单击左箭头激活)对定位文件和获取文件路径非常有用。它允许您单击已安装的文件夹结构并复制文件路径,请参见下图。

          【讨论】:

            【解决方案6】:

            这是一个简单的方法。您可以在 Python 中使用 wget 命令或 requests 模块来完成工作。

            # find the share link of the file/folder on Google Drive
            file_share_link = "https://drive.google.com/open?id=0B_URf9ZWjAW7SC11Xzc4R2d0N2c"
            
            # extract the ID of the file
            file_id = file_share_link[file_share_link.find("=") + 1:]
            
            # append the id to this REST command
            file_download_link = "https://docs.google.com/uc?export=download&id=" + file_id 
            

            file_download_link中的字符串可以粘贴到浏览器地址栏,直接得到下载对话框。

            如果你使用 wget 命令:

            !wget -O ebook.pdf --no-check-certificate "$file_download_link"
            

            【讨论】:

              【解决方案7】:

              不安装/导入任何库。只需将您的文件 ID 放在末尾即可。

              !gdown --id yourFileIdHere
              

              注意:在撰写本文时,gdown 库已预安装在 colab 上。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-04-05
                • 1970-01-01
                • 2020-08-03
                • 2016-09-24
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多