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
, thePath
,PurePath
orPureWindowsPath
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()
). ThereforePath("c:/temp/one/two/").name
will returntwo
, becausetwo
is merely a sub-directory inone
(thePath
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 aPath
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'