【问题标题】:how to get the file path for the ipython notebook in use? (the equivalent to __file__)如何获取正在使用的 ipython 笔记本的文件路径? (相当于 __file__)
【发布时间】:2019-06-19 19:29:45
【问题描述】:

我想将目录更改为我的 jupyter 笔记本的父目录。

我无法使用 os.path.basename(os.path.dirname(os.path.realpath(__file__))) 获取笔记本路径,因为未定义 __file__

如何获取我正在使用的 ipynb 文件的目录以便 os.chdir() 访问它?

【问题讨论】:

    标签: python jupyter


    【解决方案1】:

    你不能

    https://github.com/ipython/ipython/issues/10123

    原因是因为您始终在内核中运行,理论上多个笔记本可以连接到该内核。

    但是 - 默认情况下,如果您正在启动笔记本,则当前工作目录设置为笔记本的路径。所以最接近的是打电话给os.getcwd()

    我刚刚创建了最无聊的published notebook example to demonstrate this,你可以看到,this notebook reflects it's paththis one that's in a subdirectory also reflects the proper path

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    相关资源
    最近更新 更多