Benefits of os.path.splitext over regular .split?

前端 未结 11 927
独厮守ぢ
独厮守ぢ 2020-12-08 13:16

In this other question, the votes clearly show that the os.path.splitext function is preferred over the simple .split(\'.\')[-1] string manipulatio

相关标签:
11条回答
  • 2020-12-08 13:42

    Besides being standard and therefore guaranteed to be available, os.path.splitext:

    Handles edge cases - like that of a missing extension.
    Offers guarantees - Besides correctly returning the extension if one exists, it guarantees that root + ext will always return the full path.
    Is cross-platform - in the Python source there are actually three different version of os.path, and they are called based on which operating system Python thinks you are on.
    Is more readable - consider that your version requires users to know that arrays can be indexed with negative numbers.

    btw, it should not be any faster.

    0 讨论(0)
  • 2020-12-08 13:47

    The first and most obvious difference is that the split call has no logic in it to default when there is no extension.

    This can also be accomplished by a regex, in order to get it to behave as a 1 liner without extra includes, but still return, empty string if the extension isn't there.

    Also, the path library can handle different contexts for paths having different seperators for folders.

    0 讨论(0)
  • 2020-12-08 13:49

    splitext() does a reverse search for '.' and returns the extension portion as soon as it finds it. split('.') will do a forward search for all '.' characters and is therefore almost always slower. In other words splitext() is specifically written for returning the extension unlike split().

    (see posixpath.py in the Python source if you want to examine the implementation).

    0 讨论(0)
  • 2020-12-08 13:49

    A clearly defined and documented method to get the file extension would always be preferred over splitting a string willy nilly because that method would be more fragile for various reasons.

    Edit: This is not language specific.

    0 讨论(0)
  • 2020-12-08 13:50

    There exist operating systems that do not use ‘.’ as an extension separator.

    (Notably, RISC OS by convention uses ‘/’, since ‘.’ is used there as a path separator.)

    0 讨论(0)
  • 2020-12-08 13:58

    1) simple split('.')[-1] won't work correctly for the path as C:\foo.bar\Makefile so you need to extract basename first with os.path.basename(), and even in this case it will fail to split file without extension correctly. os.path.splitext do this under the hood.

    2) Despite the fact os.path.splitext is cross-platform solution it's not ideal. Let's looking at the special files with leading dot, e.g. .cvsignore, .bzrignore, .hgignore (they are very popular in some VCS as special files). os.path.splitext will return entire file name as extension, although it does not seems right for me. Because in this case name without extension is empty string. Although this is intended behavior of Python standard library, it's may be not what user wants actually.

    0 讨论(0)
提交回复
热议问题