python - os.path.expanduser ("~/x") 是否等同于 os.path.abspath(os.path.expanduser ("~/x"))?

标签 python path cross-platform absolute-path

在 Python 中,如果路径中包含“~”,我能否确定扩展的用户调用将是绝对路径?

例如,这个表达式是否总是为真?

path = '~/.my_app'
os.path.expanduser(path) == os.path.abspath(os.path.expanduser(path))

最佳答案

这取决于您的$HOME 指向什么。在大多数正确设置的系统(每个主流 Linux 发行版、OSX 和 Windows)上,它会指向一个绝对路径,例如/home/userC:/Users/User。但如果它未设置、设置不当甚至手动更改(export HOME=.),expanduser 可能会导致相对路径,在这种情况下 abspath 将进一步改变它。

但对于大多数意图和目的,您可以假设是的,这两个表达式是等价的。

关于python - os.path.expanduser ("~/x") 是否等同于 os.path.abspath(os.path.expanduser ("~/x"))?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21317010/

相关文章:

c++ - 绝对路径...困惑(ubuntu)

c++ - (bool)(i & 1) 和 i % 2 == 1 相同吗?

python - 属性错误: module 'tensorflow.python.framework.op_def_registry' has no attribute 'get_registered_ops'

python - 为什么不使用 pip 安装?

python - 如何停止这个循环?告诉用户他们输入的最长字符串的最佳方式是什么?

bash - 如何安装要在终端中使用的 Dart? (命令行)

xml - 代表C :\Program Files in XML

c++ - 在 Windows 和 Linux 上构建 C++

audio - 跨平台音频分析库

python - MongoDB M0集群,python多线程