【问题标题】:Accessing a folder in Google Drive from Colaboratory从 Colaboratory 访问 Google Drive 中的文件夹
【发布时间】:2020-01-01 08:53:47
【问题描述】:

我希望能够从 Colaboratory 读取文件并将文件写入 Google Drive 中的文件夹,但不知道如何访问特定文件夹。

我成功挂载了我的驱动器,然后做了一个文件列表。清单显示了我感兴趣的目录(为简洁起见进行了编辑),因此我尝试切换到该目录但出现错误。

 from google.colab import drive
    drive.mount('/content/drive')

    Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True)

import os
os.listdir()

输出(已编辑)

['99 温度', '02 工作', '01 家', '家庭建筑-关键事实-Sheet.pdf', 'Home-Contents-Key-Facts-Sheet.pdf', ……, 'Travel Insurance Allianz Certificate of Insurance.pdf', '数据']

os.chdir("drive/Data")
os.listdir()

*FileNotFoundError                         Traceback (most recent call last)
<ipython-input-25-853a98f4629a> in <module>()
----> 1 os.chdir("drive/Data")
      2 os.listdir()
FileNotFoundError: [Errno 2] No such file or directory: 'drive/Data'*

【问题讨论】:

    标签: python directory google-drive-api google-colaboratory


    【解决方案1】:

    如果您想移动到已编辑输出的最后一个元素中列出的“数据”目录,请尝试此操作。看起来你已经在/content/drive

    os.chdir("Data")
    os.listdir()
    

    您可以通过pwd 命令确认您当前的工作目录。

    !pwd
    

    【讨论】:

      【解决方案2】:

      我的回答是基于假设 Google Drive 中的“Data”文件夹保存在 My Drive 文件夹下(默认情况下对于 Google Drive 这是正常的)

      import os
      os.chdir("/content/drive/My Drive/Data")
      
      
      !pwd
      
      

      【讨论】:

        猜你喜欢
        • 2018-12-30
        • 2019-01-14
        • 2018-06-10
        • 2019-05-21
        • 2018-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-16
        相关资源
        最近更新 更多