【问题标题】:Error in changing the directory to google drive even when mounted in google colab即使安装在 google colab 中,也将目录更改为 google 驱动器时出错
【发布时间】:2021-10-08 23:35:50
【问题描述】:

代码

import os
os.chdir('/content/drive/MyDrive/laptop_1.jpg')

%cd '/content/drive/MyDrive/laptop_1.jpg'

错误

NotADirectoryError: [Errno 20] Not a directory: '/content/drive/MyDrive/laptop_1.jpg'

我已经在 Colab 上尝试了这两个代码多次,但错误仍然相同。有人可以提出一些解决方案吗?

【问题讨论】:

  • 安装驱动时colab请求授权

标签: python-3.x google-colaboratory


【解决方案1】:

错误信息很清楚:'/content/drive/MyDrive/laptop_1.jpg' 不是目录路径,而是文件,而os.chdir() 只接收目录路径。您需要将代码更改为:

import os
os.chdir('/content/drive/MyDrive')

注意:如果您在 Google Colab 笔记本中,MyDrive/content/gdrive 的子目录。此外,您需要先安装 Google Drive。在这种情况下:

from google.colab import drive
import os

drive.mount('/content/gdrive') # mounting your Google Drive to your Colab notebook
os.chdir('/content/gdrive/MyDrive') # changing the working directory

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 2023-01-19
    • 2019-08-09
    • 2023-01-07
    • 2020-03-29
    • 2021-08-29
    • 2019-06-20
    • 2020-11-10
    相关资源
    最近更新 更多