Repository navigation
Open
Labels
3.10 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2018-10-08.12:06:01.079>
labels = ['type-bug', 'library', '3.10']
title = 'os.path.splitext with more dots'
updated_at = <Date 2022-01-03.23:23:53.237>
user = 'https://bugs.python.org/xnovakj'bugs.python.org fields:
activity = <Date 2022-01-03.23:23:53.237>
actor = 'iritkatriel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2018-10-08.12:06:01.079>
creator = 'xnovakj'
dependencies = []
files = []
hgrepos = []
issue_num = 34931
keywords = ['patch']
message_count = 13.0
messages = ['327346', '328722', '328810', '328820', '370262', '409569', '409575', '409576', '409615', '409617', '409618', '409622', '409625']
nosy_count = 9.0
nosy_names = ['pitrou', 'xnovakj', 'serhiy.storchaka', 'eryksun', 'Malcolm Smith', 'lys.nikolaou', 'miss-islington', 'xtreak', 'iritkatriel']
pr_nums = ['30347', '30368', '30369']
priority = 'normal'
resolution = None
stage = 'resolved'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue34931'
versions = ['Python 3.10']Metadata
Metadata
Assignees
Labels
3.10 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsTodo
Activity
xnovakj commentedon Oct 8, 2018
There are some old tickets about changing splitext() in 2007:
https://bugs.python.org/issue1115886
https://bugs.python.org/issue1681842
Present python documentation:
Leading periods on the basename are ignored; splitext('.cshrc') returns ('.cshrc', '').
Changed in version 2.6: Earlier versions could produce an empty root when the only period was the first character.
But nobody take care about more than one dots:
For example this possible corect filenames:
>>> os.path.splitext('....jpg') ('....jpg', '')So present function is insuficient (buggy) to use to detect right extension.
Maybe new parameter would be helpfull for that?
Like parameter "preserve_dotfiles" discussed in 2007.
And what to do with the wrong '.', '..', '...', ... filenames?
lysnikolaou commentedon Oct 28, 2018
IMHO this is not a bug. Every file starting with a dot is hidden by default, so this is somewhat correct behaviour. Since it is documented correctly I don't this something needs to change here.
Yet again, could someone more experienced offer their opinion as well?
xnovakj commentedon Oct 29, 2018
Yes, dot behaviour is well documented.
But splitext() is a function to split name and extension, not take care about hidden files.
Hidden files starting with dot is Unix like system feature.
https://en.wikipedia.org/wiki/Hidden_file_and_hidden_directory
Hidden files could have also extension.
Try for example create/rename .somename.jpg file in Ubuntu.
It is normal image with .jpg extension. You could open it etc.
You can't use standard python splitext() function to detect extension.
I know that change this standard python function is probably imposible due to backward compatibility. Bud extend it with new parameter could be possible. The change in 2007 was unfortunate/incomplete.
serhiy-storchaka commentedon Oct 29, 2018
Related issues: bpo-536120, bpo-1115886, bpo-1462106, bpo-1681842, bpo-19191.
Python-Dev discussions:
mhsmith commentedon May 28, 2020
Yes you can (Python 3.8.2):
>>> splitext(".somename.jpg") ('.somename', '.jpg')Only leading dots are treated specially. If there are non-leading dots later in the name, then the filename will be split at the last one.
So the only remaining question is weird filenames like ".....jpg". There's no way to know whether that's supposed to be a name with an extension, or a dot-file with multiple leading dots. Either choice would be reasonable, but we've already gone with the second one.
Maybe sometimes you might prefer the other way, but such filenames are so rare that I can't imagine we'd ever add an option for this. So I think this issue can be closed.
serhiy-storchaka commentedon Jan 3, 2022
There are other issues with the documentation of splitext().
It uses term "extension" (it is even a part of function name), but it is vague and usually does not include a period. On Windows the extension of "python.exe" is "exe", not ".exe". On Unix term "suffix" is commonly used, ".exe" is a suffix. It is also used in pathlib. I suggest to replace "extension" with "suffix".
It is not specified that only the part of the last path component is included in the suffix, and leading periods of the last path component are ignored, not just leading periods of the path. So splitext('mail.dir/') == ('mail.dir/', '') and splitext('/home/user/.etc') is ('/home/user/.etc', ''). It is not documented that splitext() works with multi-component paths at all.
eryksun commentedon Jan 3, 2022
FWIW, a file extension in Windows includes the dot. Trailing dots are stripped from filenames, so a file can't be named "python." because it's the same as just "python". (It's possible to prevent stripping trailing dots from a name by using a \\?\ literal path, but creating such a filename is a bad idea.)
The shell API's file associations include the dot in the file extension. Also, the PATHEXT environment variable (i.e. the list of extensions that a CLI shell should try appending in a PATH search) includes the dot in each extension. In both of the latter cases, an extension that's just "." matches a filename that has no extension. In other words, the "." file extension can be used to associate files that have no extension with a ProgID (i.e. a programmatic identifier, which defines properties and actions for a file type), and adding "." to PATHEXT includes files that have no extension in a PATH search.
To clarify further, here are some results from PathCchFindExtension() [1]:
>>> s.value = 'python.exe' >>> _ = path.PathCchFindExtension(s, len(s), ctypes.byref(ext)) >>> ext.value '.exe' >>> s.value = '...exe' >>> _ = path.PathCchFindExtension(s, len(s), ctypes.byref(ext)) >>> ext.value '.exe' >>> s.value = 'python.' >>> _ = path.PathCchFindExtension(s, len(s), ctypes.byref(ext)) >>> ext.value '.'[1] https://docs.microsoft.com/en-us/windows/win32/api/pathcch/nf-pathcch-pathcchfindextension
serhiy-storchaka commentedon Jan 3, 2022
Well, so we can keep term "extension". But I think it is worth to clarify that "leading periods" is related to the last component, not the whole path. It is related to the original issue.
iritkatriel commentedon Jan 3, 2022
New changeset 51700bf by Irit Katriel in branch 'main':
bpo-34931: [doc] clarify behavior of os.path.splitext() on paths with multiple leading periods (GH-30347)
51700bf
iritkatriel commentedon Jan 3, 2022
New changeset 8184a61 by Miss Islington (bot) in branch '3.10':
bpo-34931: [doc] clarify behavior of os.path.splitext() on paths with multiple leading periods (GH-30347) (GH-30368)
8184a61
iritkatriel commentedon Jan 3, 2022
New changeset 4a792ca by Miss Islington (bot) in branch '3.9':
bpo-34931: [doc] clarify behavior of os.path.splitext() on paths with multiple leading periods (GH-30347) (GH-30369)
4a792ca
xnovakj commentedon Jan 3, 2022
Thank you all for discussion and partial solution in latest Python versions and extending documentation.
For the future development of Python the initial question remains.
How to easy detect extensions for each file with standard python library function. Without programing own function to fix it.
Filenames with more dots could exist both in Unix and Windows worlds.
Nobody can't say (for example web app users). Please not use those files.
There are some possible ways:
Thank you
xnovakj commentedon Jan 3, 2022
It is interesting that pathlib.Path works fine: