How/where 要使用 os.path.sep?
How/where to use os.path.sep?
os.path.sep
是操作系统用来分隔路径名组成部分的字符。
但是在os.path.join()
中使用os.path.sep
时,为什么会截断路径?
示例:
而不是'home/python'
,os.path.join
returns '/python'
:
>>> import os
>>> os.path.join('home', os.path.sep, 'python')
'/python'
我知道 os.path.join()
会隐式插入目录分隔符。
os.path.sep
有什么用?为什么会截断路径?
Where os.path.sep is usefull?
我怀疑它存在的主要原因是模块中无论如何都需要像这样的变量(以避免硬编码),如果它存在,它也可能被记录下来。它的文档说它是 "occasionally useful".
Why it truncates the path?
If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
和 /
是 *nix 系统上的绝对路径。
从 os.path.join()
通话中删除 os.path.sep
。 os.path.join()
在内部使用 os.path.sep
。
在您的系统上,os.path.sep == '/'
被解释为根目录(绝对路径),因此 os.path.join('home', '/', 'python')
等同于 os.path.join('/', 'python') == '/python'
。 From the docs:
If a component is an absolute path, all previous components are thrown
away and joining continues from the absolute path component.
如 os.path.join
-
的文档字符串中正确给出
Join two or more pathname components, inserting '/' as needed. If any component is an absolute path, all previous path components will be discarded.
docs中也给出了相同的-
os.path.join(path, *paths)
Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
当你单独给出 os.path.sep
时,它被认为是根目录的绝对路径 - /
.
请注意,这是基于 unix/linux 的 os.path,内部是 posixpath
。尽管在 windows os.path.join()
.
中可以看到相同的行为
例子-
>>> import os.path
>>> os.path.join.__doc__
"Join two or more pathname components, inserting '/' as needed.\n If any component is an absolute path, all previous path components\n will be discarded."
如果您在 POSIX
机器上,这是 运行 的代码片段:
# Join pathnames.
# Ignore the previous parts if a part is absolute.
# Insert a '/' unless the first part is empty or already ends in '/'.
def join(a, *p):
"""Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded. An empty last part will result in a path that
ends with a separator."""
sep = _get_sep(a)
path = a
try:
if not p:
path[:0] + sep #23780: Ensure compatible data type even if p is null.
for b in p:
if b.startswith(sep):
path = b
elif not path or path.endswith(sep):
path += b
else:
path += sep + b
except (TypeError, AttributeError, BytesWarning):
genericpath._check_arg_types('join', a, *p)
raise
return path
具体来说,行:
if b.startswith(sep):
path = b
而且,由于 os.path.sep
肯定是从这个字符开始的,所以每当我们遇到它时,我们都会丢弃变量 path
中已经构造的部分,并从 [ 中的下一个元素重新开始=15=].
But when os.path.sep is used in os.path.join() , why it truncates the path?
直接引用os.path.join
的文档
If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
所以当你这样做时:
os.path.join('home', os.path.sep, 'python')
os.path.sep
returns '/'
这是一个绝对路径,所以 'home'
被丢弃,你只得到 '/python'
作为输出。
从例子中也可以看出:
>>> import os
>>> os.path.join('home','/python','kivy')
'/python/kivy'
Where os.path.sep is usefull?
os.path.sep
或 os.sep
returns 操作系统用来分隔路径名组件的字符。
但再次引用 docs:
Note that knowing this is not sufficient to be able to parse or concatenate pathnames — use os.path.split() and os.path.join() — but it is occasionally useful.
os.path.sep
是操作系统用来分隔路径名组成部分的字符。
但是在os.path.join()
中使用os.path.sep
时,为什么会截断路径?
示例:
而不是'home/python'
,os.path.join
returns '/python'
:
>>> import os
>>> os.path.join('home', os.path.sep, 'python')
'/python'
我知道 os.path.join()
会隐式插入目录分隔符。
os.path.sep
有什么用?为什么会截断路径?
Where os.path.sep is usefull?
我怀疑它存在的主要原因是模块中无论如何都需要像这样的变量(以避免硬编码),如果它存在,它也可能被记录下来。它的文档说它是 "occasionally useful".
Why it truncates the path?
If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
和 /
是 *nix 系统上的绝对路径。
从 os.path.join()
通话中删除 os.path.sep
。 os.path.join()
在内部使用 os.path.sep
。
在您的系统上,os.path.sep == '/'
被解释为根目录(绝对路径),因此 os.path.join('home', '/', 'python')
等同于 os.path.join('/', 'python') == '/python'
。 From the docs:
If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
如 os.path.join
-
Join two or more pathname components, inserting '/' as needed. If any component is an absolute path, all previous path components will be discarded.
docs中也给出了相同的-
os.path.join(path, *paths)
Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
当你单独给出 os.path.sep
时,它被认为是根目录的绝对路径 - /
.
请注意,这是基于 unix/linux 的 os.path,内部是 posixpath
。尽管在 windows os.path.join()
.
例子-
>>> import os.path
>>> os.path.join.__doc__
"Join two or more pathname components, inserting '/' as needed.\n If any component is an absolute path, all previous path components\n will be discarded."
如果您在 POSIX
机器上,这是 运行 的代码片段:
# Join pathnames.
# Ignore the previous parts if a part is absolute.
# Insert a '/' unless the first part is empty or already ends in '/'.
def join(a, *p):
"""Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded. An empty last part will result in a path that
ends with a separator."""
sep = _get_sep(a)
path = a
try:
if not p:
path[:0] + sep #23780: Ensure compatible data type even if p is null.
for b in p:
if b.startswith(sep):
path = b
elif not path or path.endswith(sep):
path += b
else:
path += sep + b
except (TypeError, AttributeError, BytesWarning):
genericpath._check_arg_types('join', a, *p)
raise
return path
具体来说,行:
if b.startswith(sep):
path = b
而且,由于 os.path.sep
肯定是从这个字符开始的,所以每当我们遇到它时,我们都会丢弃变量 path
中已经构造的部分,并从 [ 中的下一个元素重新开始=15=].
But when os.path.sep is used in os.path.join() , why it truncates the path?
直接引用os.path.join
的文档If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
所以当你这样做时:
os.path.join('home', os.path.sep, 'python')
os.path.sep
returns '/'
这是一个绝对路径,所以 'home'
被丢弃,你只得到 '/python'
作为输出。
从例子中也可以看出:
>>> import os
>>> os.path.join('home','/python','kivy')
'/python/kivy'
Where os.path.sep is usefull?
os.path.sep
或 os.sep
returns 操作系统用来分隔路径名组件的字符。
但再次引用 docs:
Note that knowing this is not sufficient to be able to parse or concatenate pathnames — use os.path.split() and os.path.join() — but it is occasionally useful.