The challenge I faced while working on a project was: I needed only the filename, not the full directory path. If you’ve ever worked with file systems in Python, you know how often this comes up.
For example, when processing reports from multiple states in the USA, you may have file paths like:
C:\Users\John\Documents\Reports\Texas_Sales.xlsxBut in most cases, you only need the filename:
Texas_Sales.xlsxAt first, I tried splitting strings manually, but I quickly realized Python has built-in modules that make this task much easier. In this tutorial, I’ll show you four simple methods to get filename from path in Python.
Python Get Filename From Path
There are four Python methods for getting the file name from the path: the ‘os’ module, the ‘pathlib’ module, split() and rsplit(), and regular expressions.
Method 1: Use the Os Module
The ‘os’ module in Python allows you to interact with operating systems. It has several functions that can be used to interact with operating system file systems.
The OS module contains a function called basename() in the submodule os.path. You must pass this function’s path to get the file name.
file_path = "/home/user/documents/data.txt"The file name is ‘data.txt’, so you can use the basename() function to extract this name from the path, as shown in the code below.
file_name = os.path.basename(file_path)Now print the ‘file_name’ using the code below.
print(file_name)You can see the output in the screenshot below.

Similarly, if you access a different file path, you can get the file name using the basename() function of the submodule os.path.
Method 2: Use Pathlib Module in Python
The module ‘pathlib’ is a new module alternative to ‘os’, introduced in Python 3.4; it uses object-oriented concepts to file path systems.
To get the file name using the ‘pathlib’, you must use this module’s class ‘Path’. This class represents the file system path and provides different functions to operate on these paths, such as reading or writing content.
from pathlib import PathThen, define the file path as shown in the code below.
file_path = "/home/user/documents/editor.exe"Then, pass the file_path to the Path() function, as shown in the code below.
path = Path(file_path)After that, to get the file name, call the attribute ‘name’ on the ‘path’ as shown in the code below.
print(path.name)You can see the output in the screenshot below.

Look at the output: when the attribute ‘name’ is called on the ‘path’, it returns the file’s name from the path “/home/user/documents/editor.exe”.
Method 3: Use Python Regular Expression
A regular expression consists of a sequence of characters, which acts as a pattern to search for a string or a subset of a string.
You can use a pattern to extract the file name from the path. The pattern is given below.
[\w]+?(?=\.)Where,
- [\w] : It matches the words within the set
- +? : It match the string if it’s present only once before? keyword
- (?=): It matches all characters without a newline and ensures to stops at.
For example, you have a path and pattern, which is defined below,
path = "/home/user/documents/data.zip"
pattern = '[\w-]+?(?=\.)'To extract the file name, search the pattern in the specified path “/home/user/documents/data.zip”. As you can see, the path is the string value, so to search this pattern, you will need to use the Python search() function of the ‘re’ module.
import re
file_name = re.search(pattern, path)To match the pattern to the string path, call the group() method on the ‘file_name’ as shown in the code below.
print(file_name.group())You can see the output in the screenshot below.

This is not the best method for extracting the file name from the path, but you can use it if you have no other choice. This is how to get the name of the file using a regular expression.
Method 4: Use Python’s Split() and Resplit() Method
The split() and rsplit() are two functions in Python that split the string into substrings based on the specified separator.
There is a subtle difference between split() and rsplit ().
- split(separator, maxsplit): It splits the string into a list of substrings based on the specified separator; if the separator is not provided, it splits the string based on whitespace by default.
- rsplit (separator,maxsplit): The working of the rsplit() is the same as the rsplit(), but it splits the string from the right side of the string, or in simpler words, it starts the splitting of the string from the end.
For example, define the file’s path as shown in the code below.
path = "/home/user/documents/sales.tar"Then call the split() with separator as ‘\’ to divide the path into a list of substrings using the code below.
path_substring = path.split('/')
print(path_substring)Then, take the last substring using the ‘path_substring[-1]’, then call the rsplit() with separator as dot (.) and perform splitting once by specifying the maxsplit as 1 shown in the code below.
file_name = path_substring[-1].rsplit('.', 1)
print(file_name)After executing the above code, the ‘file_name’ contains the file name and extension as a list of substrings. The file name is at the 0th index in the list, so to access it, use the code below.
print(file_name[0])You can see the output in the screenshot below.

This is how to print the file in Python using the split() and rsplit() functions together.
Conclusion
In this Python tutorial, you learned about Python to get a filename from a path using the four methods: the ‘os’ module, the ‘pathlib’ module, split() and rsplit (), and regular expressions.
You may like to read:
- Python Dict Methods
- Convert Dict to String in Python
- Get All Values From a Dictionary in Python
- Python Get First Key in Dictionary

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.
Hi Bijay,
that solution to get file name appears incorrect as per: https://docs.python.org/3/library/os.path.html#os.path.basename
and the below from IDLE:
Python 3.8.2 (default, Jul 16 2020, 14:00:26)
>>> import os
>>> print(os.path.basename(‘E:\project-python\string\list.py’))
E:\project-python\string\list.py
>>>
I hope it helps.
ignore last comment, I tested the windows path in a linux interpreter… duh!