在 os.path.basename 的参数中使用尾随目录定界符会产生空字符串是否有原则性原因?除了看起来像未处理的实现边缘情况或实现副作用之外,我不确定我能否理解该返回值。
>>> import os
>>> os.path.basename('/tmp')
'tmp'
>>> os.path.basename('/tmp/')
''
请您参考如下方法:
basename 的文档说明了这一点:
Return the base name of pathname path. This is the second element of the pair returned by passing path to the function split().
os.path.split 的文档是这样说的(添加了强调):
Split the pathname path into a pair, (head, tail) where tail is the last pathname component and head is everything leading up to that. The tail part will never contain a slash; if path ends in a slash, tail will be empty.
在 "/tmp/" 上调用 os.path.split 会产生 ('/tmp', '')。因为该对的第二个元素是 '',所以 basename 返回 ''。
