为什么 pathlib.Path(__file__).parent.parent 对我的工作目录敏感?

问题描述

我有一个包含两个目录的脚本。

❯ tree
.
└── foo
    └── bar
        └── test.py
❯ cd foo/bar
❯ cat test.py

    from pathlib import Path
    print(Path(__file__).parent)
    print(Path(__file__).parent.parent)

当我从包含它的目录运行它时,PathLib 认为文件的祖父文件与其父文件相同。

❯ python test.py

    . # <-- same
    . # <-- directories

但是当我从顶层运行它时,PathLib 的行为是正确的。

❯ cd ../..
❯ python foo/bar/test.py

    foo/bar # <-- different
    foo     # <-- directories

我对 PathLib 的 API 有什么误解,还是其他原因导致其输出对我的工作目录敏感?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)