为什么 os.normpath 不折叠前导双斜杠?
在 Unix 下,os.path.normpath 将多个斜杠折叠为单个斜杠除非路径开头恰好出现两个斜杠。为何例外?
为了说明这一点,我得到了以下转换:
//double/slash/stays -> //double/slash/stays
/double/slash//gone// -> /double/slash/gone/
double//slash//gone/ -> double/slash/gone
///triple/slash/gone -> /triple/slash/gone
////quad/slash/gone -> /quad/slash/gone
这对我来说似乎很奇怪。我可以模糊地想象这对于 SMB 安装或 URL 很有用,但我不认为我关心这些。 Python 的行为是否有任何隐藏的智慧,或者我应该自己折叠领先的 // ?
[更新] 鉴于下面的答案,看起来最好的办法不是折叠 //,而是要么接受它,要么将其视为错误。
Under Unix, os.path.normpath collapses multiple slashes into single ones except when exactly two slashes appear that the start of the path. Why the exception?
To illustrate, I get the following transformations:
//double/slash/stays -> //double/slash/stays
/double/slash//gone// -> /double/slash/gone/
double//slash//gone/ -> double/slash/gone
///triple/slash/gone -> /triple/slash/gone
////quad/slash/gone -> /quad/slash/gone
This seems strange to me. I can vaguely imagine this is useful for SMB mounts or URLS, but I don't think I care about those. Is there any hidden wisdom to Python's behaviour, or should I just collapse the leading // myself?
[update]
In view of the answer below, it looks like the best thing is not to collapse the //, but to either just accept it, or to treat it as an error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为 POSIX 允许以实现定义的方式处理以两个斜杠开头的路径。换句话说,在所有 POSIX 系统上,//foo 不一定与 /foo 含义相同。
来自IEEE Std 1003.1:
另请参阅此错误报告(已作为“不是错误”关闭)。
Because POSIX allows treating a path beginning with two slashes in an implementation-defined manner. In other words, //foo does not necessarily mean the same thing as /foo on all POSIX systems.
From IEEE Std 1003.1:
See also this bug report (which was closed as "not a bug").