首页 文章

Colaboratory:我可以访问我的Google Cloud 端硬盘文件夹和文件吗?

提问于
浏览
15

谷歌Colaboratory真的很酷,但如果我可以访问我的所有谷歌驱动器文件,而不使用标准的谷歌驱动器API将更有用 .

是否可能而且容易?如何?

4 回答

  • 3

    以下是使用FUSE Drive界面访问Drive文件(如本地文件)的示例:https://colab.research.google.com/notebook#fileId=1srw_HFWQ2SMgmWIawucXfusGzrj1_U0q

    简而言之:

    # Load the Drive helper and mount
    from google.colab import drive
    drive.mount('/content/drive')
    

    执行上述代码后,您的驱动器文件将出现在 /content/drive/My Drive 中 .

    我猜你也找到了捆绑的示例I / O笔记本,它展示了如何使用Python API来访问文件 . (这需要更少的配置 . )https://colab.research.google.com/notebook#fileId=/v2/external/notebooks/io.ipynb&scrollTo=c2W5A2px3doP

  • 10

    请按照colab笔记本中的3个简单步骤访问google驱动器中的文件夹或文件,

    Step :1 运行此代码段

    !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
        !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
        !apt-get update -qq 2>&1 > /dev/null
        !apt-get -y install -qq google-drive-ocamlfuse fuse
        from google.colab import auth
        auth.authenticate_user()
        from oauth2client.client import GoogleCredentials
        creds = GoogleCredentials.get_application_default()
        import getpass
        !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
        vcode = getpass.getpass()
        !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
    

    在此步骤中,您将被要求两次单击链接以允许访问您的驱动器,在每个步骤将生成代码:(例如:4 / AACN9EZG2AU0dRsV0BupjAc107ugSvT_pmr4YPElX7VkoWru6mNmqc8) . 粘贴此代码并单击Enter .

    Step:2 创建目录

    !mkdir -p drive
    !google-drive-ocamlfuse drive
    

    Step:3 访问您的文件

    import pandas as pd
    trainDf = pd.read_csv("drive/app/Sample/train.csv");//Here is your file
    
  • 3

    成功运行Drive FUSE程序后,您可以使用using命令访问/ content / drive中的驱动器

    import os
    os.chdir("/content/drive/")
    !ls
    
  • 25

    是的,你可以这样做 .

    按照以下步骤操作 .

    Run the below code and complete the authentication

    !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
    !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
    !apt-get update -qq 2>&1 > /dev/null
    !apt-get -y install -qq google-drive-ocamlfuse fuse
    from google.colab import auth
    auth.authenticate_user()
    from oauth2client.client import GoogleCredentials
    creds = GoogleCredentials.get_application_default()
    import getpass
    !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
    vcode = getpass.getpass()
    !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
    

    运行以下代码

    !mkdir -p drive
    !google-drive-ocamlfuse drive
    

    导入文件到笔记本

    import pandas as pd
    pd.read_csv("drive/Colab_Notebooks/4k_without_spcl.csv")
    

    Colab_Notebooks is folder in google drive

相关问题