Python Get Filename from Path: Practical Methods and Examples

Oct 04, 2025 at 05:05 am by johnusa


Working with files is a common task in Python, and one of the frequent requirements is to extract the filename from a complete file path. Instead of manually splitting strings, Python offers reliable built-in modules that simplify this process. The most commonly used approaches include the os module and the pathlib module. Both are efficient, and the choice often depends on coding style and project needs.

Using os.path.basename
The os module has been part of Python for a long time and provides useful functions for handling paths. One of the simplest ways to extract the filename is with os.path.basename(). For example, given a path like /home/user/data/report.csv, calling os.path.basename(path) will return report.csv. This method is quick and works consistently across different operating systems.

Using os.path.split
Another function in the same module is os.path.split(). This splits the path into two parts: the directory and the filename. For instance, splitting /home/user/data/report.csv will give ('/home/user/data', 'report.csv'). This is useful when you need both the folder path and the filename separately.

Using pathlib.Path.name
For developers who prefer a more modern and cleaner syntax, the pathlib module is often the better choice. Introduced in Python 3.4, pathlib uses an object-oriented approach to path handling. Extracting the filename is as simple as Path(path).name. For example, Path("/home/user/data/report.csv").name will return report.csv. This approach is widely appreciated for its readability and flexibility.

Using pathlib.Path.stem
In some cases, you may want the filename without the extension. The stem attribute in pathlib helps with this. For the path /home/user/data/report.csv, calling Path(path).stem returns report. This is especially useful when preparing filenames for processing, renaming, or generating outputs without file extensions.

Choosing Between os and pathlib

  • If you are working on older code or scripts that already use os, sticking with os.path.basename() makes sense.

  • If you are writing new projects, pathlib is often recommended because it is more intuitive and handles cross-platform paths smoothly.

Practical Applications

  • Extracting filenames for logging or monitoring file operations

  • Storing only the file name in a database instead of the full path

  • Preparing filenames for batch processing or renaming tasks

  • Cleaning up data pipelines where filenames are needed for identification

Important Notes

  • Always use raw strings (r"path") when dealing with Windows file paths to avoid escape sequence errors.

  • Both methods are cross-platform, but pathlib reduces the chance of inconsistencies between Windows and Linux paths.

In summary, the python get filename from path task can be handled easily with either os or pathlib. Whether you need just the filename, both the directory and filename, or even the filename without its extension, Python provides straightforward solutions to keep your file handling clean and reliable.

Sections: Other News