Python splitext

bla*_*bla 12 python operating-system path separator

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

Tim*_*ker 5

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)
Run Code Online (Sandbox Code Playgroud)

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))
Run Code Online (Sandbox Code Playgroud)

另外,来自genericpath.py:

def _get_dot(path):
    if isinstance(path, bytes):
        return b'.'
    else:
        return '.'
Run Code Online (Sandbox Code Playgroud)

所以,os.path()事实上确实定义两次延长分离.

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