Why doesn't os.normpath collapse a leading double slash?

妖精的绣舞 提交于 2019-12-07 04:37:48

问题


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.


回答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:

A pathname that begins with two successive slashes may be interpreted in an implementation-defined manner, although more than two leading slashes shall be treated as a single slash.

See also this bug report (which was closed as invalid).



来源:https://stackoverflow.com/questions/7816818/why-doesnt-os-normpath-collapse-a-leading-double-slash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!