【发布时间】:2019-06-19 19:29:45
【问题描述】:
我想将目录更改为我的 jupyter 笔记本的父目录。
我无法使用 os.path.basename(os.path.dirname(os.path.realpath(__file__))) 获取笔记本路径,因为未定义 __file__。
如何获取我正在使用的 ipynb 文件的目录以便 os.chdir() 访问它?
【问题讨论】:
我想将目录更改为我的 jupyter 笔记本的父目录。
我无法使用 os.path.basename(os.path.dirname(os.path.realpath(__file__))) 获取笔记本路径,因为未定义 __file__。
如何获取我正在使用的 ipynb 文件的目录以便 os.chdir() 访问它?
【问题讨论】:
你不能
https://github.com/ipython/ipython/issues/10123
原因是因为您始终在内核中运行,理论上多个笔记本可以连接到该内核。
但是 - 默认情况下,如果您正在启动笔记本,则当前工作目录设置为笔记本的路径。所以最接近的是打电话给os.getcwd()
我刚刚创建了最无聊的published notebook example to demonstrate this,你可以看到,this notebook reflects it's path和this one that's in a subdirectory also reflects the proper path
【讨论】: