In this Python tutorial, we will discuss, different ways to get file name from a path in Python. Not only this, I will also show you how to get the file extension from the file name in Python. Finally, we will discuss, how to get the file name without extension in Python.
How to get the file name from Path in Python
Here we will see at least 3 different ways to get the file name from path in Python.
Let’s consider an example file path:
sample.txt is the file name that we are interested in extracting. Below, we will explore different methods to achieve this.
Method 1: Using os.path.basename()
os module in Python provides a plethora of functions to interact with the operating system. The
os.path.basename() function can be used to extract the filename from a path.
import os file_path = "/home/user/documents/sample.txt" file_name = os.path.basename(file_path) print(file_name) # Output: sample.txt
os.path.basename() function takes the file path as an argument and returns the base name, i.e., the file name. Check the screenshot below for the output:
Method 2: Using pathlib.Path()
pathlib is a module in Python 3, which is object-oriented and a more modern approach to handling filesystem paths. It’s recommended to use
os.path for improved readability and simplicity.
from pathlib import Path file_path = Path("/home/user/documents/sample.txt") file_name = file_path.name print(file_name) # Output: sample.txt
In this example, we create a
Path object with the file path as an argument and then use the
name attribute to obtain the file name.
Check the screenshot below for the output:
Method 3: Using string splitting
This method is less reliable and not recommended for use in production code, but for the sake of completeness, we can also split the string to extract the filename.
file_path = "/home/user/documents/sample.txt" file_name = file_path.split('/')[-1] print(file_name) # Output: sample.txt
Here, we split the file path string by the delimiter
/ and select the last element in the resulting list. This method is not recommended because it relies on a fixed delimiter and can cause issues with different operating systems.
Handling Edge Cases
The methods above work well for standard file paths, but what if the file path ends with a directory separator (
\ depending on the OS)? In such cases, the first two methods are robust enough to handle them, but the string splitting method will fail.
Let’s demonstrate this with an example:
import os from pathlib import Path file_path = "/home/user/documents/directory/" # Using os.path.basename() file_name_os = os.path.basename(file_path.rstrip(os.path.sep)) print(file_name_os) # Output: directory # Using pathlib.Path() file_path_obj = Path(file_path) file_name_pathlib = file_path_obj.name if file_path_obj.is_dir() else file_path_obj.parent.name print(file_name_pathlib) # Output: directory
os.path.basename() method can handle this by stripping the directory separator at the end before processing. For
pathlib, we check if the path is a directory; if so, we extract the name, otherwise, we use the parent directory’s name.
You may like the following Python tutorials:
- Read a file line by line in Python
- Python Program to Write List to File
- How to read a CSV file in Python
In this Python tutorial, we discuss how to get the file name from the path in Python.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.