. Advertisement .
. Advertisement .
This tutorial will show you how to get filename from path in Python. This operation works seamlessly in any platform and filesystem.
Get Filename From Path In Python
In addition to quitting a program, the os module also provides common manipulation capabilities for pathnames. For example, the os.path.basename() function can return the base name (the final part) of a path. It accepts a path-like object and returns a string.
import os.path path = ['/home/User/Downloads', '/home/User/Downloads/tutorial.html', 'C:\Programing Files\Python\python.exe', '../doc.pdf', 'example.py'] for i in path: print(os.path.basename(i))
Downloads tutorial.html C:\Programing Files\Python\python.exe doc.pdf example.py
You can see that the os.path.basename() can strip away unnecessary parts and leave only the end of each path name. The code above is run on a Linux system, which couldn’t process a Windows path by default.
However, it is possible to import the module ntpath to manipulate Windows paths. It works regardless of the platform you are running the code on.
import ntpath windows_path = ['C:\Programing Files\Python\python.exe', 'D:\Documents\/assignment.tex'] for i in windows_path: print(ntpath.basename(i))
Remember that os.path.basename() can’t work with non-path arguments. This will show an error like this:
>>> import os.path >>> os.path.basename(3) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.10/posixpath.py", line 142, in basename p = os.fspath(p) TypeError: expected str, bytes or os.PathLike object, not int
If you want to get only the name of a file without its extension, check out the os.path.splitext() function. It splits a file name into two parts: the root and extension.
import os.path path = ['/home/User/Downloads/tutorial.html', '/home/User/Downloads/index.html', '~/about.html'] for i in path: base = os.path.basename(i) print(os.path.splitext(base))
tutorial index about
If you have Python version 3.4 or above, the pathlib module can help you get filename from path in Python as well.
This module is created to represent and process filesystem paths. There are two path classes within the pathlib module: concrete paths (which can perform I/O operations) and pure paths (without any I/O capabilities).
With pure path objects, you can handle path operations without accessing the filesystem. Here is the version of the first code that uses Purepath class.
from pathlib import PurePath path = ['/home/User/Downloads', '/home/User/Downloads/tutorial.html', '../doc.pdf', 'example.py'] for i in path: print(PurePath(i).name)
Downloads tutorial.html doc.pdf
The .name property represents the final component of each path, which is basically the same as os.path.basename().
If you want to get the file name without its extension, use the .stem property.
from pathlib import PurePath path = ['/home/User/Downloads', '/home/User/Downloads/tutorial.html', '../doc.pdf', 'example.py'] for i in path: print(PurePath(i).stem)
Downloads tutorial doc
The PurePosixPath and PureWindowsPath classes allow you to handle file paths of another operating system.
from pathlib import PureWindowsPath windows_path = ['C:\Programing Files\Python\python.exe', 'D:\Documents\/assignment.tex'] for i in windows_path: print(PureWindowsPath(i).name)
example.py python.exe assignment.tex
You can use either the os.path or pathlib module to get filename from path in Python. The latter was added recently in Python 3.4 and has been the recommended option by many developers.