os.path.normpath 和符号链接

os.path.normpath and symbolic links

os.path.normpath 的文档内容如下:

This string manipulation may change the meaning of a path that contains symbolic links.

什么情况和例子适用于此处,意思会发生变化?这个函数应用在什么地方会不安全,解决方法是什么?

免责声明: 我对此不是 100% 确定,现在无法访问 *nix 系统进行检查。你可能想检查一下,或者等到评论者告诉我我是对还是错 ;-)

也就是说,这是我的答案:

在给定的示例中 A/foo/../B -> A/B。如果没有符号链接,一切都很好。

使用相同的例子,假设:

  • 我们目前在根目录/
  • A在根目录(/A)
  • A/foo 是指向目录 /X/Y
  • 的符号链接
  • /X/B是一个目录

所以:

  1. A/foo/../B 表示 "the directory B, which is in the parent directory of A/foo".
  2. A/foo 是 /X/Y 的符号链接,所以 "the directory B, which is in the parent directory of A/foo" 实际上意味着 "the directory B, which is in the parent directory of /X/Y"
  3. /X/Y的父目录是/X,所以"the directory B, which is in the parent directory of /X/Y"真正的意思是"the directory B, which is in /X"。
  4. 所以A/foo/../B实际上是指/X/B.

在这种情况下,A/foo/../B/A/B 的规范化是不正确的,它应该规范化到 /X/B