Both functions use the os.path.split(path) function to split the pathname path into a pair; (head, tail).

The os.path.dirname(path) function returns the head of the path.

E.g.: The dirname of '/foo/bar/item' is '/foo/bar'.

The os.path.basename(path) function returns the tail of the path.

E.g.: The basename of '/foo/bar/item' returns 'item'

From: http://docs.python.org/3/library/os.path.html#os.path.basename

Answer from Breno Teixeira on Stack Overflow
🌐
Python
docs.python.org › 3 › library › os.path.html
os.path — Common pathname manipulations
This is the second element of the pair returned by passing path to the function split(). Note that the result of this function is different from the Unix basename program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an empty string ('').
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-os-path-basename-method
Python | os.path.basename() method - GeeksforGeeks
February 23, 2026 - os.path.basename() method in Python is used to extract the final part of a file path. This final part can be a file name or the last folder name.
🌐
Python
docs.python.org › 3 › library › pathlib.html
pathlib — Object-oriented filesystem paths
Many functions in os and os.path support bytes paths and paths relative to directory descriptors. These features aren’t available in pathlib. Python’s str and bytes types, and portions of the os and os.path modules, are written in C and are very speedy.
🌐
Tutorialspoint
tutorialspoint.com › python › os_path_basename.htm
Python os.path.basename() Method
In this example, we are extracting the filename "file.txt" from the URL using the basename() method − · import os url = "https://example.com/path/to/file.txt" filename = os.path.basename(url) print(filename)
🌐
Medium
medium.com › @johnusa4567 › python-get-filename-from-path-simple-methods-explained-a5ea7623b38c
Python Get Filename from Path: Simple Methods Explained | by John Usa | Medium
October 4, 2025 - Python provides built-in modules ... and the pathlib module. Using os.path.basename The os module has a function called basename() that returns the final component of a path....
🌐
Astral
docs.astral.sh › ruff › rules › os-path-basename
os-path-basename (PTH119) | Ruff
When possible, using Path object ... os.path.basename()). ... While using pathlib can improve the readability and type safety of your code, it can be less performant than the lower-level alternatives that work directly with strings, especially on older versions of Python...
Find elsewhere
Top answer
1 of 16
1984

There's a function that returns exactly what you want

import os
print(os.path.basename(your_path))

WARNING: When os.path.basename() is used on a POSIX system to get the base name from a Windows-styled path (e.g. "C:\\my\\file.txt"), the entire path will be returned.

Example below from interactive python shell running on a Linux host:

Python 3.8.2 (default, Mar 13 2020, 10:14:16)
[GCC 9.3.0] on Linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> filepath = "C:\\my\\path\\to\\file.txt" # A Windows style file path.
>>> os.path.basename(filepath)
'C:\\my\\path\\to\\file.txt'
2 of 16
1070

Using os.path.split or os.path.basename as others suggest won't work in all cases: if you're running the script on Linux and attempt to process a classic windows-style path, it will fail.

Windows paths can use either backslash or forward slash as path separator. Therefore, the ntpath module (which is equivalent to os.path when running on windows) will work for all(1) paths on all platforms.

import ntpath
ntpath.basename("a/b/c")

Of course, if the file ends with a slash, the basename will be empty, so make your own function to deal with it:

def path_leaf(path):
    head, tail = ntpath.split(path)
    return tail or ntpath.basename(head)

Verification:

>>> paths = ['a/b/c/', 'a/b/c', '\\a\\b\\c', '\\a\\b\\c\\', 'a\\b\\c', 
...     'a/b/../../a/b/c/', 'a/b/../../a/b/c']
>>> [path_leaf(path) for path in paths]
['c', 'c', 'c', 'c', 'c', 'c', 'c']


(1) There's one caveat: Linux filenames may contain backslashes. So on linux, r'a/b\c' always refers to the file b\c in the a folder, while on Windows, it always refers to the c file in the b subfolder of the a folder. So when both forward and backward slashes are used in a path, you need to know the associated platform to be able to interpret it correctly. In practice it's usually safe to assume it's a windows path since backslashes are seldom used in Linux filenames, but keep this in mind when you code so you don't create accidental security holes.

🌐
Read the Docs
python.readthedocs.io › fr › latest › library › os.path.html
11.2. os.path — Common pathname manipulations — documentation Python 3.7.0a0
This is the second element of the pair returned by passing path to the function split(). Note that the result of this function is different from the Unix basename program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an empty string ('').
🌐
Python
docs.python.org › id › 3.5 › library › os.path.html
11.2. os.path --- Common pathname manipulations — dokumentasi Python 3.5.10
This is the second element of the pair returned by passing path to the function split(). Note that the result of this function is different from the Unix basename program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an empty string ('').
🌐
Medium
medium.com › @ryan_forrester_ › understanding-pythons-basename-complete-guide-7730ea4235fb
Understanding Python’s basename: Complete Guide | by ryan | Medium
November 5, 2024 - The basename function, found in the `os.path` module, extracts the last component of a file path.
🌐
Python.org
discuss.python.org › ideas
Clarify if os.path.basename is equivalent to pathlib.Path.name - Ideas - Discussions on Python.org
October 15, 2024 - within the documentation the table tells us that os.path.basename() is equivalent to PurePath.name however, this SO thread states According to the Python documentation os.path.basename simply uses .split(). It therefore is a fairly rudimentary implementation.
🌐
GeeksforGeeks
geeksforgeeks.org › python › os-path-module-python
OS Path Module in Python - GeeksforGeeks
July 1, 2026 - 1. os.path.basename(path): returns the final component of a path, usually the file or folder name. Python · import os out = os.path.basename("/projects/report.txt") print(out) Output · report.txt · Explanation: os.path.basename() extracts ...
🌐
Stack Abuse
stackabuse.com › bytes › get-file-name-from-path-for-any-os-in-python
Get File Name from Path for any OS in Python
September 5, 2023 - To handle this, we can use the os.path.basename() function along with the raw string literal (r). The r prefix before the string literal converts the normal string to a raw string.
🌐
YouTube
youtube.com › watch
Python Basics OS Path Basename Method - YouTube
Learn how use basename method from os.path module for python programming. Basename returns the file name from the path twitter: @python_basicsprerequisite:a...
Published: January 2, 2019
🌐
Educative
educative.io › answers › what-is-ospathbasename-method-in-python
What is os.path.basename() method in Python?
Operating System modules provide ... different places of a local file system. We use the os.path.basename()function to extract a specified path name's base name....