12

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

4

1 回答 1

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)

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 原则。

于 2011-09-16T14:03:50.190 回答