Use os.path.splitext:

>>> import os
>>> filename, file_extension = os.path.splitext('/path/to/somefile.ext')
>>> filename
'/path/to/somefile'
>>> file_extension
'.ext'

Unlike most manual string-splitting attempts, os.path.splitext will correctly treat /a/b.c/d as having no extension instead of having extension .c/d, and it will treat .bashrc as having no extension instead of having extension .bashrc:

>>> os.path.splitext('/a/b.c/d')
('/a/b.c/d', '')
>>> os.path.splitext('.bashrc')
('.bashrc', '')
Answer from nosklo on Stack Overflow
🌐
Replit
replit.com › home › discover › how to get a file extension in python
How to get a file extension in Python | Replit
March 10, 2026 - Python's built-in modules, like os and pathlib, make this simple with robust functions. In this article, we’ll cover several techniques to get file extensions. We’ll also explore practical tips, real-world applications, and debugging advice to help you choose the best method for your needs. import os.path filename = "document.pdf" file_extension = os.path.splitext(filename)[1] print(f"The file extension is: {file_extension}")--OUTPUT--The file extension is: .pdf
Discussions

How to get only the name of the path with python? - Stack Overflow
I have the following string: a = "/home/user/Downloads/repo/test.txt" My goal is just to create a string which contains only test, how can I do this ? actually a comes from f = tkFileDia... More on stackoverflow.com
🌐 stackoverflow.com
string - How do I get the filename without the extension from a path in Python? - Stack Overflow
I interpreted the question as "given a path, return the basename without the extension." ... In Python 2.7, where we still live without pathlib... More on stackoverflow.com
🌐 stackoverflow.com
python - Trouble getting file name with extension using pathlib - Stack Overflow
I have this code which gives me the filename no problem. It gives it to me without the extension. Any way to get with extension? from pathlib import Path file = 'somepath' path_object = Path(file) More on stackoverflow.com
🌐 stackoverflow.com
Is there any situation where string.endswith() would not be suitable for checking for a specific file extension?
They don't behave the same: >>> from os.path import splitext >>> splitext('pic.jpg')[1] == '.jpg', 'pic.jpg'.endswith('.jpg') (True, True) >>> splitext('....jpg')[1] == '.jpg', '....jpg'.endswith('.jpg') (False, True) There's also a bit of weirdness with regard to names like archive.tar.gz. You might even consider checking like this to play it safe: >>> from pathlib import Path >>> Path('archive.tar.gz').suffixes == ['.tar', '.gz'] True >>> Path('pic.png').suffixes == ['.png'] True More on reddit.com
🌐 r/learnpython
14
2
March 15, 2023
🌐
FavTutor
favtutor.com › blogs › get-filename-from-path-python
03 Methods to Get Filename from Path in Python (with code)
December 16, 2022 - Now we can use the built-in split function in python to split the path name. We will split the path name at every \. Once we do that, we will have a tuple that contains all the words between the slashes.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get the Filename, Directory, Extension from a Path String in Python | note.nkmk.me
May 8, 2023 - UNIX (including Mac) uses the slash ... · source: os_path_basename_dirname_split_splitext.py · Use os.path.basename() to get the filename (basename) from a path string....
🌐
Medium
medium.com › @emoome82 › extracting-file-names-without-extensions-in-python-caabe8532f92
Extracting File Names Without Extensions in Python | by civilpy | Medium
July 30, 2024 - # Define a file path file_path ... file_name_with_extension = os.path.basename(file_path)# Split the file name and extension file_name, extension = os.path.splitext(file_name_with_extension)print(f'File name (without extension): {file_name}') print(f'Extension: {extension}') ... Python ...
Find elsewhere
🌐
Vultr
docs.vultr.com › python › examples › extract-extension-from-the-file-name
Python Program to Extract Extension From the File Name | Vultr Docs
November 25, 2024 - Note that if dealing with compound extensions like .tar.gz, additional logic would be needed to capture the entire extension. Import the pathlib module. Use the suffix property on a Path object to get the extension.
🌐
TradingCode
tradingcode.net › python › filename-without-last-extension
Get filename with extension removed: here's how in Python
Python has several ways to get a folder’s content. One option is the Path.iterdir() method. This method returns all items of a directory, but won’t go into subdirectories. So we only get the files and folders in the directory itself. To get the filename without the last extension from those directory items, here’s what we do: from pathlib import Path # Specify the directory to get the filenames without extension from dir_path = Path(r"C:\Wallpapers") # Loop over all directory entries.
🌐
Python
docs.python.org › 3 › library › pathlib.html
pathlib — Object-oriented filesystem paths
February 23, 2026 - dirpath is a Path to the directory ... To get a full path (which begins with self) to a file or directory in dirpath, do dirpath / name....
🌐
Python Shiksha
python.shiksha › home › tips › 2 ways to extract/get extension from the file name in python
2 ways to extract/get extension from the file name in Python - Python Shiksha
August 1, 2022 - Pathlib is one of the most used python modules when it comes to playing around with the file paths. By passing the filename as a string parameter to the pathlib.Path() function, we can create a new Path() object which has three values to be accessed.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › extract extension from filename in python
Extract Extension from filename in Python - Spark By {Examples}
May 31, 2024 - # Quick Examples of extracting ... from a filename using the split() method in Python, we can split the filename into a list of substrings using the dot (.) character as the delimiter and then select the last item in the ...
🌐
datagy
datagy.io › home › file handling › python: get filename from path (windows, mac & linux)
Python: Get Filename From Path (Windows, Mac & Linux) • datagy
February 10, 2023 - Want to learn how to get a file’s extension in Python? This tutorial will teach you how to use the os and pathlib libraries to do just that! We can get a filename from a path using only string methods, in particular the str.split() method. This method will vary a bit depending on the operating system you use. In the example below, we’ll work with a Mac path and get the filename:
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-get-file-extension-in-python
How to get file extension in Python? - GeeksforGeeks
July 23, 2025 - Use the os.path Module to Extract Extension From File in Python · Use the pathlib Module to Extract Extension From File in Python
🌐
Quora
quora.com › How-do-I-get-the-filename-without-the-extension-from-a-path-in-Python
How to get the filename without the extension from a path in Python - Quora
Pure path object: Path(path).stem returns the final file name without its last suffix. ... Use the standard library (pathlib or os.path).
🌐
TutorialsPoint
tutorialspoint.com › how-to-extract-file-extension-using-python
How to extract file extension using Python?
The os.path.splitext() method of the os module in Python is used to split the file name into the name and extension. This method helps us extract the extension part easily from the file name. In this example, we are using the os.path.splitext() method to get the extension of the given file ...
🌐
Python Guides
pythonguides.com › get-file-extension-from-file-name-in-python
How to Get File Extension in Python?
September 30, 2025 - Similarly, you can get any file extension using the splitext() function of the os.path submodule. The ‘pathlib’ module is an alternative to the ‘os’ module; this module was introduced in Python 3.4. The ‘pathlib’ module has a class ...