目次
1. What is os.path.basename()?
Python’sos.path.basename()
is a function that extracts the file name portion from a specified file path. For example, given a path like “/home/user/documents/file.txt”, os.path.basename()
returns “file.txt”. This function is useful in many situations as a cross-platform file operation tool that works across different operating systems.Overview of os.path.basename()
os.path.basename() is a function in theos
module that extracts the last component of a file path (usually the file name). This feature is useful when you want to use the full path but extract only the file name.Basic usage example
import os
file_path = "/home/user/documents/report.pdf"
file_name = os.path.basename(file_path)
print(file_name) # Output: report.pdf
This code extracts the file name “report.pdf” from the specified file path. It is used in many situations by everyone from Python beginners to professionals.2. How to use os.path.basename()
From here, we’ll explain how to useos.path.basename()
in practice and go over common usage patterns.How to get the filename from a file path
If you pass a full path toos.path.basename()
, it returns the filename at the end of the path. Since it returns the filename including the extension, you can display it as-is or use it in other processing.Sample code
import os
file_path = "/home/user/documents/report.pdf"
file_name = os.path.basename(file_path)
print(file_name) # Output: report.pdf
How to get a directory name from a directory path
If you pass a directory path (rather than a file path) toos.path.basename()
, it returns the final element—the directory name. In the example below, “/home/user/documents/” returns “documents”.Sample code
import os
dir_path = "/home/user/documents/"
directory_name = os.path.basename(dir_path)
print(directory_name) # Output: documents
Getting a filename including its extension
os.path.basename()
returns the filename including its extension. This makes it simple and practical, since you don’t have to check whether an extension exists before getting the filename.Sample code
import os
file_path = "/path/to/data/file.csv"
file_name_with_extension = os.path.basename(file_path)
print(file_name_with_extension) # Output: file.csv

3. Understanding the differences between os.path.basename() and other related functions
os.path
The module contains several other useful functions for path manipulation besides basename()
. Below, we’ll compare with the commonly used os.path.dirname()
, os.path.split()
, os.path.splitext()
.Difference from os.path.dirname()
os.path.dirname()
retrieves the directory path, whereasos.path.basename()
retrieves the file name.
Difference from os.path.split()
os.path.split()
can return both the directory path and the file name at the same time.
Combining with os.path.splitext()
- Using
os.path.splitext()
, you can easily remove the extension from a file name.
4. Make getting filenames easier with pathlib
Since Python 3.4, thepathlib
module for file path operations has been standardized. It is more intuitive than os.path
and is particularly useful for projects that prioritize readability.How to use pathlib.Path.name
from pathlib import Path
file_path = Path("/home/user/documents/report.pdf")
print(file_path.name) # Output: report.pdf
With pathlib.Path.name
, you can easily obtain filenames and perform the same operations as basename()
in an object-oriented way.
5. Real-world Examples
Let’s look at the examples below to see howos.path.basename()
is used in real projects.Renaming Files
import os
directory = "/path/to/files"
new_prefix = "new_file"
for index, filename in enumerate(os.listdir(directory)):
old_file = os.path.join(directory, filename)
if os.path.isfile(old_file):
new_file_name = f"{new_prefix}_{index}{os.path.splitext(filename)[1]}"
new_file = os.path.join(directory, new_file_name)
os.rename(old_file, new_file)
6. Precautions and Best Practices
When usingos.path.basename()
, be mindful of OS-specific path separator differences and of using it together with pathlib
. Pay attention to the following precautions.- Use
os.path.join()
andpathlib.Path()
for cross-platform compatibility - Verify file existence with
os.path.exists()
oros.path.isfile()
7. Summary
os.path.basename()
is a very handy function for getting the filename from a file path in Python. This article covered the basic usage of basename()
as well as related functions and how it differs from pathlib
.- Getting filenames to cross-platform compatibility, you should now understand how this can be helpful in many practical scenarios.
- Next steps: consider exploring other
os.path
functions or usingpathlib
— they can make file operations even more convenient.