【问题标题】:Google Colab: "No such file or directory" when folder clearly existsGoogle Colab:当文件夹明确存在时“没有这样的文件或目录”
【发布时间】:2019-11-03 19:52:34
【问题描述】:

我正在尝试从 Google Colab 访问我放入 Google Drive 中的数据集(通过安装我的驱动器并使用 DATADIR 变量指定文件夹的路径);但是,当尝试对 DATADIR 变量执行操作时,它说目录不存在。

我尝试了其他将数据集导入 Colab 的方法,但这些方法在我使用的代码中存在自己的问题。我宁愿只安装我的 Google Drive。

这是我安装驱动器的方式:

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

这是我得到错误的地方:

DATADIR = "content/drive/My Drive/Colab Notebooks/Datasets/Cats and Dogs"
CATEGORIES = ["Dog", "Cat"]

for category in CATEGORIES:
  path = os.path.join(DATADIR, category)
  for img in os.listdir(path):
    img_array = cv2.imread(os.path.join(path, img), cv2.imread_grayscale)

这是错误信息(图片):

FileNotFoundError: [Errno 2] No such file or directory: 'content/drive/My Drive/Colab Notebooks/Datasets/Cats and Dogs/Dog'

出现在这一行:for img in os.listdir(path):

文件夹存在的证明:当我在 Colab 中单击“文件”时,我可以轻松导航到该文件夹​​。

【问题讨论】:

  • 可能在DataDir 中缺少最初的/
  • 谢谢!如果您将此作为答案发布,我可以接受。

标签: python dataset google-colaboratory


【解决方案1】:

重新启动笔记本,然后在使用脚本的单元格内写入:!ls 它将显示正确的路径。我注意到即使您移动脚本,目录也保持不变

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 2019-07-08
    • 2020-01-31
    • 2021-12-05
    • 2013-11-28
    • 1970-01-01
    相关资源
    最近更新 更多