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 OverflowPython 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 ·
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
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
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
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
Videos
05:21
How to Extract Extension From the File Name? | Python Program ...
05:21
How to Get File Extension in Python | Python Get File Extension ...
00:40
How to extract extension from file name in Python - YouTube
python how to get file extension
00:25
Python Challenge - Extract extension from the Filename #Shorts ...
06:37
How to Get File Extension in Python? | Python Program | Python ...
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.
Top answer 1 of 16
2685
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', '')
2 of 16
763
New in version 3.4.
Copyimport pathlib
print(pathlib.Path('/foo/bar.txt').suffix)
# Outputs: .txt
print(pathlib.Path('/foo/bar.txt').stem)
# Outputs: bar
print(pathlib.Path("hello/foo.bar.tar.gz").suffixes)
# Outputs: ['.bar', '.tar', '.gz']
print(''.join(pathlib.Path("hello/foo.bar.tar.gz").suffixes))
# Outputs: .bar.tar.gz
print(pathlib.Path("hello/foo.bar.tar.gz").stem)
# Outputs: foo.bar.tar
I'm surprised no one has mentioned pathlib yet, pathlib IS awesome!
DigitalOcean
digitalocean.com › community › tutorials › get-file-extension-in-python
How to Get File Extension in Python | DigitalOcean
August 3, 2022 - We can use Python os module splitext() function to get the file extension.
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.
Naukri
naukri.com › code360 › library › how-to-get-filename-extensions-in-python
How To Get Filename Extensions In Python?
Almost there... just a few more seconds
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.
Reddit
reddit.com › r/learnpython › how to get file type/extension?
r/learnpython on Reddit: how to get file type/extension?
November 14, 2021 -
is there any builtin function to get the file's type even though there is no extension to the file?
Top answer 1 of 2
2
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/
2 of 2
1
Nope. There's no reliable way to get the type of a file without looking at the file extension. Some file formats do have the file type stored in the first couple bytes of the file (like PNG 7z, PDF) which you could read, but many formats don't have that information and it isn't even guaranteed to be accurate. For example, using this method you won't be able to tell Word documents, Excel spreadsheets, and ZIP archives apart because they're all technically the same type. There's also tons of plaintext file formats that you won't be able to detect without parsing the whole document.
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.