【问题标题】: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 上尝试了这两个代码多次,但错误仍然相同。有人可以提出一些解决方案吗?
【问题讨论】:
标签:
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