Which Python library can I use to extract filenames from paths, no matter what the operating system or path format could be?
For example, I’d like all of these paths to return me c:
a/b/c/
a/b/c
\a\b\c
\a\b\c\
a\b\c
a/b/../../a/b/c/
a/b/../../a/b/c
Using
os.path.splitoros.path.basenameas others suggest won’t work in all cases: if you’re running the script on Linux and attempt to process a classic windows-style path, it will fail.Windows paths can use either backslash or forward slash as path separator. Therefore, the
ntpathmodule (which is equivalent to os.path when running on windows) will work for all(1) paths on all platforms.Of course, if the file ends with a slash, the basename will be empty, so make your own function to deal with it:
Verification:
(1) There’s one caveat: Linux filenames may contain backslashes. So on linux,
r'a/b\c'always refers to the fileb\cin theafolder, while on Windows, it always refers to thecfile in thebsubfolder of theafolder. So when both forward and backward slashes are used in a path, you need to know the associated platform to be able to interpret it correctly. In practice it’s usually safe to assume it’s a windows path since backslashes are seldom used in Linux filenames, but keep this in mind when you code so you don’t create accidental security holes.