Well, there are separate implementations for separate operating systems. This means that if the logic to extract the extension of a file differs on Mac from that on Linux, this distinction will be handled by those things. I don't know of any such distinction so there might be none.


Edit: @Brian comments that an example like /directory.ext/file would of course not work with a simple .split('.') call, and you would have to know both that directories can use extensions, as well as the fact that on some operating systems, forward slash is a valid directory separator.

This just emphasizes the use a library routine unless you have a good reason not to part of my answer.

Thanks @Brian.


Additionally, where a file doesn't have an extension, you would have to build in logic to handle that case. And what if the thing you try to split is a directory name ending with a backslash? No filename nor an extension.

The rule should be that unless you have a specific reason not to use a library function that does what you want, use it. This will avoid you having to maintain and bugfix code others have perfectly good solutions to.

Answer from Lasse V. Karlsen on Stack Overflow
🌐
Python
docs.python.org › 3 › library › os.path.html
os.path — Common pathname manipulations
>>> splitext('foo.bar.exe') ('foo.bar', '.exe') >>> splitext('/foo/bar.exe') ('/foo/bar', '.exe')
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-os-path-splitext-method
Python os.path.splitext() method - GeeksforGeeks
July 11, 2025 - DSA Python · Data Science · NumPy ... method is used to split the pathname into a pair (root, ext), where root is the part of the path before the file extension and ext is the file extension itself....
Discussions

"os.path.splitext(filename)" seems to be randomly assinging the split, how do I define what is filename, and what is extension?
That's not the fault of splitext(), that's because you are using a set to store the pairs, which is by definition unordered. Use a tuple instead: driveO_files.append((name, file_extension)) Edit: A tuple happens to be what splitext returns in the first place, so it would be a lot easier to append that directly, rather than unpacking and repacking it: driveO_files = [] for filename in os.listdir(driveO): driveO_files.append(os.path.splitext(filename)) Or driveO_files = [os.path.splitext(filename) for filename in os.listdir(driveO)] More on reddit.com
🌐 r/learnpython
4
1
January 25, 2021
Separating file extensions using python os.path module - Stack Overflow
But it actually does not work that way. splitext will improperly split filenames with more than one dot. bugs.python.org/issue34931 2021-11-13T15:50:02.09Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
`os.path.splitext()` can split UNC drive on Windows
Bug report >>> import ntpath >>> ntpath.splitext('//server/share.jpg') ('//server/share', '.jpg') # expected: ('//server/share.jpg', '') This path has an empty basename, and so its suffix should also be empty. Affects all current Python ... More on github.com
🌐 github.com
5
February 24, 2024
Better `splitext` function.
Feature or enhancement I wanted to help and change the inner workings of the splitext function to work with multiple file extensions. In most cases when working with archives it tend to have archiv... More on github.com
🌐 github.com
5
March 13, 2023
🌐
Tutorialspoint
tutorialspoint.com › python › os_path_splitext.htm
Python os.path.splitext() Method
The Python os.path.splitext() method is used to split a pathname into two parts: "root" and "ext". The "root" contains the filename without its extension, while "ext" holds the extension part of the filename, including the dot (.).
🌐
Astral
docs.astral.sh › ruff › rules › os-path-splitext
os-path-splitext (PTH122) | Ruff
import os (root, ext) = os.path.splitext("foo/bar.py") ... 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.
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › os.path.splitext.html
os.path.splitext() — Python Standard Library
os.path.splitext() View page source · os.path.splitext(p)[source]¶ · Split the extension from a pathname. Extension is everything from the last dot to the end, ignoring leading dots.
Find elsewhere
🌐
Pynerds
pynerds.com › python-os-path-splitext-function
Python os.path.splitext() Function
October 25, 2023 - The splitext() function in the path submodule is used to split a pathname into a tuple containing the filename and the file extension. The name of the function is a shortened form for "split extension".
🌐
Reddit
reddit.com › r/learnpython › "os.path.splitext(filename)" seems to be randomly assinging the split, how do i define what is filename, and what is extension?
r/learnpython on Reddit: "os.path.splitext(filename)" seems to be randomly assinging the split, how do I define what is filename, and what is extension?
January 25, 2021 -

I am trying to create a script that will create a dataframe of files in a directory and create two columns 'FileName' and 'Extension'.

I thought I was getting somewhere, but the output is not what I am expecting at all. Or more specifically, it is, but is an unexpected manner.

This is my current code:

import os
import pandas as pd
import pyodbc

driveO = "O:\Labels"

driveO_files = []

for filename in os.listdir(driveO):
    name, file_extension = os.path.splitext(filename)
    driveO_files.append({name, file_extension})

driveO_files = pd.DataFrame(driveO_files,columns=['FileName','Extension'])
print(driveO\_files.head())
btw_filesO = driveO_files.loc[driveO_files['Extension'] =='.btw']
print(btw\_filesO.head())

Expected output:

{Index} FileName Extension
0 LabelDesing1 btw
1 LabelDesing1 dat
2 LabelDesign2 btw
3 LabelDesign2 dat
4 LabelDesign3 btw

However, what I am getting is:

{Index} FileName Extension
0 LabelDesing1 btw
1 dat LabelDesing1
2 LabelDesign2 btw
3 LabelDesign2 dat
4 btw LabelDesign3

It is random which items get put on which side, and changes each time I run the script.

How do I tell os.path.splitext() which side of the dot to put into which vatiable?

For what its worth:

  • There are no files that do not have an extension.

  • There is one folder within the list that always seems to go on the 'Extension' side.

  • There are only 65 items in this list at present.

Any help is appreciated.

EDIT: Layout adjustments

🌐
DigitalOcean
digitalocean.com › community › tutorials › get-file-extension-in-python
How to Get File Extension in Python | DigitalOcean
August 3, 2022 - Here is a simple program to get the file extension in Python. import os # unpacking the tuple file_name, file_extension = os.path.splitext("/Users/pankaj/abc.txt") print(file_name) print(file_extension) print(os.path.splitext("/Users/pankaj/.bashrc")) print(os.path.splitext("/Users/pankaj/a.b/image.png"))
🌐
Not Defined Tech
notdefined.tech › blog › splitting-extension-from-filenames-in-python
The Best Ways to Separate Filename and Extension in Python
March 26, 2025 - >>> path, extension = splitext('filename.txt') # For clarity, here's what gets stored: >>> path 'filename' >>> extension '.txt'
🌐
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 - import os filename = "example.jpeg" root, ext = os.path.splitext(filename) print("File Extension:", ext) Explain Code
🌐
GitHub
github.com › python › cpython › issues › 115892
`os.path.splitext()` can split UNC drive on Windows · Issue #115892 · python/cpython
February 24, 2024 - >>> import ntpath >>> ntpath.splitext('//server/share.jpg') ('//server/share', '.jpg') # expected: ('//server/share.jpg', '') This path has an empty basename, and so its suffix should also be empty. Affects all current Python versions. gh-115892: Fix ntpath.splitext() with UNC paths #115996 ·
Author   barneygale
🌐
GitHub
github.com › python › cpython › issues › 102672
Better `splitext` function. · Issue #102672 · python/cpython
March 13, 2023 - Feature or enhancement I wanted to help and change the inner workings of the splitext function to work with multiple file extensions. In most cases when working with archives it tend to have archive.tar.gz and the extensions are left unt...
Author   0xAndrewBlack
🌐
Stack Overflow
stackoverflow.com › questions › 70523361 › issues-getting-file-extensions-using-os-path-splitext-method
python - Issues getting file extensions using os.path.splitext() method - Stack Overflow
Thanks! I also figured out that os.path.splitext() was not working because I assigned the return value of os.path.splitext(filename)[0] to the variable called filename.
🌐
Oxylabs
oxylabs.io › home › resources › web scraping faq › python › file extension
How to Get File Extension in Python?
Use `os.path.splitext()` to reliably get the file extension, including the dot, which helps in distinguishing files without an extension from those with one. Prefer using `pathlib.Path.suffix` in modern Python (3.4+) for a more object-oriented approach, which directly provides the extension with the dot.