gpt4 book ai didi

Python 分文

转载 作者:太空狗 更新时间:2023-10-29 17:50:23 26 4
gpt4 key购买 nike

在 python 中,为什么 os.path.splitext 使用 '.'作为扩展分隔符而不是 os.extsep?

最佳答案

os.extsep 是通过导入 os.path.extsep 定义的。但你是对的,os.path.splitext() 总是使用 .,不管 os.path.extsep:

来自 os.py (3.2.2):

from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
devnull)

来自ntpath.py(变成os.path)

extsep = '.'
[...]
def _get_dot(path):
if isinstance(path, bytes):
return b'.'
else:
return '.' # instead of return extsep! [Comment by me, not in source]
[...]
def splitext(p):
return genericpath._splitext(p, _get_sep(p), _get_altsep(p),
_get_dot(p))

此外,来自genericpath.py:

def _get_dot(path):
if isinstance(path, bytes):
return b'.'
else:
return '.'

所以 os.path() 实际上确实定义了两次扩展分隔符。

现在它可能并不重要,因为它不会很快改变(无论如何在所有支持的平台上都是一样的)。但在某种程度上,它违反了 DRY 原则。

关于Python 分文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7445804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com