Extract file name from path, no matter what the os/path format
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
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
os.path.basename as 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
ntpath module (which is equivalent to os.path when running on windows) will work for all(1) paths on all platforms.
import ntpath ntpath.basename("a/b/c")
Of course, if the file ends with a slash, the basename will be empty, so make your own function to deal with it:
def path_leaf(path): head, tail = ntpath.split(path) return tail or ntpath.basename(head)
>>> paths = ['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'] >>> [path_leaf(path) for path in paths] ['c', 'c', 'c', 'c', 'c', 'c', 'c']
(1) There's one caveat: Linux filenames may contain backslashes. So on linux,
r'a/b\c' always refers to the file
b\c in the
a folder, while on Windows, it always refers to the
c file in the
b subfolder of the
a folder. 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.
Actually, there's a function that returns exactly what you want
import os print(os.path.basename(your_path))
os.path.basename() is used on a POSIX system to get the base name from a Windows styled path (e.g.
"C:\\my\\file.txt"), the entire path will be returned.
Example below from interactive python shell running on a Linux host:
Python 3.8.2 (default, Mar 13 2020, 10:14:16) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> filepath = "C:\\my\\path\\to\\file.txt" # A Windows style file path. >>> os.path.basename(filepath) 'C:\\my\\path\\to\\file.txt'
Read more... Read less...
In python 3
>>> from pathlib import Path >>> Path("/tmp/d/a.dat").name 'a.dat'
import os file_location = '/srv/volume1/data/eds/eds_report.csv' file_name = os.path.basename(file_location ) #eds_report.csv location = os.path.dirname(file_location ) #/srv/volume1/data/eds
In your example you will also need to strip slash from right the right side to return
>>> import os >>> path = 'a/b/c/' >>> path = path.rstrip(os.sep) # strip the slash from the right side >>> os.path.basename(path) 'c'
>>> os.path.filename(os.path.dirname(path)) 'b'
update: I think
lazyr has provided the right answer. My code will not work with windows-like paths on unix systems and vice versus with unix-like paths on windows system.