Skip to content

os.path.splitext with more dots #79112

Open

Description

@xnovakj
mannequin
BPO 34931
Nosy @pitrou, @serhiy-storchaka, @eryksun, @mhsmith, @lysnikolaou, @miss-islington, @tirkarthi, @iritkatriel
PRs
  • bpo-34931: [doc] clarify behavior of os.path.splitext() on paths with multiple leading periods #30347
  • [3.10] bpo-34931: [doc] clarify behavior of os.path.splitext() on paths with multiple leading periods (GH-30347) #30368
  • [3.9] bpo-34931: [doc] clarify behavior of os.path.splitext() on paths with multiple leading periods (GH-30347) #30369
  • 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']

    Activity

    1. xnovakj commented on Oct 8, 2018

      xnovakjmannequin
      MannequinAuthor

      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?

    2. added
      type-bug
      on Oct 8, 2018
    3. lysnikolaou commented on Oct 28, 2018

      @lysnikolaou
      Member

      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?

    4. xnovakj commented on Oct 29, 2018

      xnovakjmannequin
      MannequinAuthor

      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.

    5. serhiy-storchaka commented on Oct 29, 2018

      @serhiy-storchaka
      Member

      Related issues: bpo-536120, bpo-1115886, bpo-1462106, bpo-1681842, bpo-19191.

      Python-Dev discussions:

      https://mail.python.org/pipermail/python-dev/2007-March/071557.html
      https://mail.python.org/pipermail/python-dev/2007-March/071935.html
      
    6. mhsmith commented on May 28, 2020

      @mhsmith
      Member

      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.

      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.

    7. serhiy-storchaka commented on Jan 3, 2022

      @serhiy-storchaka
      Member

      There are other issues with the documentation of splitext().

      1. 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".

      2. 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.

    8. eryksun commented on Jan 3, 2022

      @eryksun
      Contributor

      On Windows the extension of "python.exe" is "exe", not ".exe".

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

          import ctypes
          path = ctypes.OleDLL('api-ms-win-core-path-l1-1-0')
          s = (ctypes.c_wchar * 100)()
          ext = ctypes.c_wchar_p()
          >>> 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

    9. serhiy-storchaka commented on Jan 3, 2022

      @serhiy-storchaka
      Member

      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.

    10. iritkatriel commented on Jan 3, 2022

      @iritkatriel
      Member

      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

    11. iritkatriel commented on Jan 3, 2022

      @iritkatriel
      Member

      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

    12. iritkatriel commented on Jan 3, 2022

      @iritkatriel
      Member

      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

    13. xnovakj commented on Jan 3, 2022

      xnovakjmannequin
      MannequinAuthor

      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.

      Python 3.10.1
      Works fine:
      >>> os.path.splitext('.some.jpg')
      ('.some', '.jpg')
      >>> os.path.splitext('..some.jpg')
      ('..some', '.jpg')
      
      Not usable:
      >>> os.path.splitext('....jpg')
      ('....jpg', '')

      There are some possible ways:

      • new parametr
      • new function
      • change backward compatibility
      • stay buggy forever

      Thank you

    14. reopened this on Jan 3, 2022
    15. xnovakj commented on Jan 3, 2022

      xnovakjmannequin
      MannequinAuthor

      It is interesting that pathlib.Path works fine:

      >>> pathlib.Path('....jpg').suffix
      '.jpg'
      >>> pathlib.Path('path/....jpg').suffix
      '.jpg'
    16. added
      stdlib
      on Jan 3, 2022
    17. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)stdlibtype-bug

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions