Colab 突然无法浏览目录

Atr*_*alb 7 python google-colaboratory

过去几个月我一直在使用 Google Colab,没有出现连接到驱动器和使用!. 但是今天突然出现错误,我找不到任何解决方法。Colab 似乎神奇地无法浏览目录。对于 cd、pwd、mkdir 等目录的任何 bash 命令,我都会收到错误消息。

即使在关闭后,重新打开笔记本并重新启动运行时,并且只运行一行代码:

!pwd
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

shell-init: error retrieving current directory: getcwd: cannot access parent directories: Transport endpoint is not connected
pwd: error retrieving current directory: getcwd: cannot access parent directories: Transport endpoint is not connected
Run Code Online (Sandbox Code Playgroud)

而您通常会/content被控制台打印出来。

我已经能够通过创建 Notebook 的副本来绕过该问题,但我仍然想问这个问题,因为在查看 Google、Stackoverflow、超级用户之后,我没有找到与我的问题密切相关的单个线程. 我也徒劳地查看了 Colab 的文档。所以我认为在未来打开一个讨论一个新问题的线程对整个社区都有好处。

有没有人遇到过这个问题并知道如何解决这个问题?

感谢大家 :)

小智 18

我遇到了类似的问题。这是对此的快速修复:

  1. 前往 Colab
  2. 单击运行时
  3. 最后点击restart runtime。(注意:你的局部变量会丢失)

现在,您将能够在 colab 单元中使用 shell 命令。


You*_*Ahn 6

不会丢失 Colab 实例中的变量,

使用 os 库更改目录。

import os
path = "/content" # /content is pretty much the root. you can choose other path in your colab workspace
os.chdir(path)
Run Code Online (Sandbox Code Playgroud)

  • 该解决方案有效,不需要重新启动运行时(并丢失所有数据)。得益于此,能够保存 9 小时后生成的 1 GB 数据。+1 (2认同)

小智 1

我也遇到了这个问题。

原因:我猜是因为目录(你所在的目录)被意外删除了。我从这里得到这个意见。然后cdpwd不再起作用了。现在我仍然可以使用python,我尝试使用

import os
# I tried to recreate the directory, it was created successfully but I still can't use the `cd` or `pwd` commands.
os.makedir('/root/the_directory_deleted') 
# then I tried to restart the kernel of colab to come back to the original path. it works!!!
os._exit(00)
Run Code Online (Sandbox Code Playgroud)

解决方案:运行上面的python函数:os.exit(00)重新启动colab内核,以恢复到有效路径。

ps: !reboot被colab禁止了