【问题标题】:How to get path that ipynb file is running in?如何获取运行 ipynb 文件的路径?
【发布时间】:2018-10-14 22:42:59
【问题描述】:

我有一个程序可以读取多个文件的子文件夹。

我之前写过的代码有:

path = 'C:/Users/Me/etc/etc/'

然后我会说打开几个文件

file1 = path + str('file1.txt')

如何更改代码以供其他人使用?我正在使用 Jupyter 笔记本,所以 __file __ 和 sys(argv[0]) 似乎不起作用。

我看过这些:

  1. How to get an absolute file path in Python
  2. How do I get the parent directory in Python?

【问题讨论】:

    标签: python file path


    【解决方案1】:

    使用获取当前工作目录,

    def join_current_dir(file):
        """Join filepath with current file directory"""
        cwd = os.getcwd()
        return os.path.join(cwd, file)
    

    【讨论】:

    • 我正在使用 Jupyter 笔记本,所以 file 不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多