Use os.path.splitext:

Copy>>> 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:

Copy>>> os.path.splitext('/a/b.c/d')
('/a/b.c/d', '')
>>> os.path.splitext('.bashrc')
('.bashrc', '')
Answer from nosklo on Stack Overflow
🌐
Python Examples
pythonexamples.org › python-get-file-extension
Get File Extension in Python
To get file extension in Python, you can use the os.path module to get the extension of a file. Call os.path.splitext() function and pass the file path as argument to the function.
🌐
Oxylabs
oxylabs.io › home › resources › web scraping faq › python › file extension
How to Get File Extension in Python?
# Import os module import os # Example file path file_path = "https://sandbox.oxylabs.io/products/data.json" # Method 1: Using os.path.splitext extension = os.path.splitext(file_path)[1] print("Extension using os.path.splitext:", extension) # Output: .json # Method 2: Using split on dot and getting the last part extension = file_path.split('.')[-1] print("Extension using split:", extension) # Output: json # Method 3: Using pathlib (for Python 3.4 and above) from pathlib import Path # Create a Path object path = Path(file_path) # Get the suffix extension = path.suffix print("Extension using pathlib:", extension) # Output: .json ·
Discussions

python - Extracting extension from filename - Stack Overflow
But it is a gzip file in first place. I wouldn't expect it to return the dual extension at all. 2014-01-16T20:18:11.35Z+00:00 ... The standard Python function naming convention is really annoying - almost every time I re-look this up, I mistake it as being splittext. More on stackoverflow.com
🌐 stackoverflow.com
I want to get the extension of uploaded file
I want to get the extension of uploaded file like( .txt, mp4, wav, .ts, .pdf, .doc, .csv, xlsx, … etc). I want that I should have a script of library which could get me the extension of the uploaded file. I am not using split() function, because there can be some files whose extension is ... More on discuss.python.org
🌐 discuss.python.org
5
0
May 7, 2024
how to get file type/extension?
That's called "content sniffing" or "mime magic". There's nothing in the Standard Library, which does have a "mimetypes" module, but that doesn't do content-sniffing. Have a look at this commonly used library: https://pypi.org/project/python-magic/ More on reddit.com
🌐 r/learnpython
6
1
November 14, 2021
Python File Type Identification
there are hundreds of thousands of file types. Some are binary and identify via their header, some are by file extension if present, sometimes you lack all information and have to guess. There is no way to tell if a file without an extension is a python file assigning a global variable as a dict or whether it is an HCL variable file for a terraform script. Even then, theres nothing stopping me using my own standard for defining something and then anything that did work will suddenly fall apart. While it is tempting to say that standards exist, none of these are defined in one place or with one version. I very much doubt you will be able to find anything that does exactly what you want in all cases. May be worth considering what types of file specifically you want to check for. More on reddit.com
🌐 r/learnpython
10
1
May 5, 2022
🌐
PythonHow
pythonhow.com › how › extract-extension-from-filename
Here is how to extract extension from filename in Python
New: Practice Python, JavaScript & SQL with AI feedback — Try ActiveSkill free → × ... You can extract the extension from a file name in Python using the os.path module, specifically the os.path.splitext() function.
🌐
Better Stack
betterstack.com › community › questions › how-to-extract-extension-from-filename-in-python
Extracting extension from filename in Python | Better Stack Community
You can use the os.path.splitext() function from the os.path module in Python to extract the file extension from a filename.
🌐
Index.dev
index.dev › blog › get-file-extension-python
3 Best Ways to Get a File Extension in Python (With Examples)
The os module provides the os.path.splitext() function, which splits a file path into the base name and its extension. This method is tried and tested, making it an ideal choice for many traditional Python projects.
Find elsewhere
🌐
datagy
datagy.io › home › file handling › python: get a file’s extension (windows, mac, and linux)
Python: Get a File's Extension (Windows, Mac, and Linux) • datagy
February 10, 2023 - Learn how to use Python to get a file extension, in Windows, Mac, and Linux. Learn how to do this with pathlib and os.path.
🌐
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 - In this article, you will learn how to efficiently extract file extensions from file names using Python.
🌐
Centron
centron.de › startseite › get file extension in python: step-by-step guide
Get File Extension in Python: Step-by-Step Guide
December 20, 2024 - Learn how to get file extensions in Python using the os module and pathlib module. Examples included for better understanding.
🌐
Python.org
discuss.python.org › python help
I want to get the extension of uploaded file - Python Help - Discussions on Python.org
May 7, 2024 - I want to get the extension of uploaded file like( .txt, mp4, wav, .ts, .pdf, .doc, .csv, xlsx, … etc). I want that I should have a script of library which could get me the extension of the uploaded file. I am not using split() function, because there can be some files whose extension is ...
🌐
Programiz
programiz.com › python-programming › examples › extract-file-extension
Python Program to Extract Extension From the File Name
Using suffix attribute from pathlib module, we can get the extension of a file. In the above example, .ext is the extension of file file.ext. Note: It works for python 3.4 and above.
🌐
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 - You can get the separator in the ... · source: os_path_basename_dirname_split_splitext.py · Use os.path.basename() to get the filename (basename) from a path string. os.path.basename() returns the filename with the ...
🌐
Scaler
scaler.com › home › topics › program to get file extension in list in python
Program to Get File Extension in List in Python - Scaler Topics
January 30, 2024 - In os. path, there is a splitext() function that allows you to separate the root and extension of a specified file path. A tuple made up of the root string and the extension string is the function's output.
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-the-file-extension-from-a-url-in-python
Get the File Extension from a URL in Python - GeeksforGeeks
July 23, 2025 - It's important to note that this approach doesn't check if the URL points to an actual file; it merely extracts the potential file extension. ... import os def get_file_extension_os(url): _, file_extension = os.path.splitext(url) return file_extension # Example usage: url = "https://example.com/path/to/file/document.pdf" extension = get_file_extension_os(url) print("File extension:", extension)
🌐
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.
🌐
DEV Community
dev.to › itsmycode › how-to-get-file-extension-in-python-4p2m
How to get file extension in Python? - DEV Community
September 12, 2021 - import os # returns tuple wit filename and extension file_details = os.path.splitext('/home/usr/sample.txt') print("File Details ",file_details) # extract the file name and extension file_name = file_details[0] file_extension = file_details[1] print("File Name: ", file_name) print("File Extension: ", file_extension) ... pathlib module comes as a standard utility module in Python and offers classes representing filesystem paths with semantics appropriate for different operating systems.