Clarify if os.path.basename is equivalent to pathlib.Path.name

within the documentation

the table tells us that os.path.basename() is equivalent to PurePath.name

however, this SO thread states

According to the Python documentation os.path.basename simply uses .split(). It therefore is a fairly rudimentary implementation. (The docs even explicitly mention to not expect the same result as for the Unix “basename” program.)

In pathlib, the Path, PurePath or PureWindowsPath objects are more sophisticated. They can represent a file, a directory, or something else. The object does not make any assumptions about what it is representing until you call its methods (e.g. .is_file()). Therefore Path("c:/temp/one/two/").name will return two, because two is merely a sub-directory in one (the Path object doesn’t care whether it is a file or directory).

therefore, to get actual parity, we should do

p = Path("c:/temp/one/two/")
("" if p.is_dir() else p.name) == ""

furthermore it states that the behavior is different if you pass in Path objects

By the way, os.path.basename does accept a Path object as a parameter. However, the behaviour is different:

Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.28.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import os

In [2]: from pathlib import Path

In [3]: os.path.basename('c:/one/two/three/')
Out[3]: ''

In [4]: os.path.basename(Path('c:/one/two/three/'))
Out[4]: 'three'

Related:

This is more a side-effect of how pathlib treats trailing slashes than anything specific to os.path.basename and Path.name. The same sort of issue pops up with e.g. os.path.dirname and Path.parent.

2 Likes