Extracting extension from filename in Python

后端 未结 24 2166
感情败类
感情败类 2020-11-22 13:23

Is there a function to extract the extension from a filename?

相关标签:
24条回答
  • 2020-11-22 14:07
    import os.path
    extension = os.path.splitext(filename)[1]
    
    0 讨论(0)
  • 2020-11-22 14:07

    Just join all pathlib suffixes.

    >>> x = 'file/path/archive.tar.gz'
    >>> y = 'file/path/text.txt'
    >>> ''.join(pathlib.Path(x).suffixes)
    '.tar.gz'
    >>> ''.join(pathlib.Path(y).suffixes)
    '.txt'
    
    0 讨论(0)
  • 2020-11-22 14:10

    Surprised this wasn't mentioned yet:

    import os
    fn = '/some/path/a.tar.gz'
    
    basename = os.path.basename(fn)  # os independent
    Out[] a.tar.gz
    
    base = basename.split('.')[0]
    Out[] a
    
    ext = '.'.join(basename.split('.')[1:])   # <-- main part
    
    # if you want a leading '.', and if no result `None`:
    ext = '.' + ext if ext else None
    Out[] .tar.gz
    

    Benefits:

    • Works as expected for anything I can think of
    • No modules
    • No regex
    • Cross-platform
    • Easily extendible (e.g. no leading dots for extension, only last part of extension)

    As function:

    def get_extension(filename):
        basename = os.path.basename(filename)  # os independent
        ext = '.'.join(basename.split('.')[1:])
        return '.' + ext if ext else None
    
    0 讨论(0)
  • 2020-11-22 14:10
    # try this, it works for anything, any length of extension
    # e.g www.google.com/downloads/file1.gz.rs -> .gz.rs
    
    import os.path
    
    class LinkChecker:
    
        @staticmethod
        def get_link_extension(link: str)->str:
            if link is None or link == "":
                return ""
            else:
                paths = os.path.splitext(link)
                ext = paths[1]
                new_link = paths[0]
                if ext != "":
                    return LinkChecker.get_link_extension(new_link) + ext
                else:
                    return ""
    
    0 讨论(0)
  • 2020-11-22 14:11

    This is a direct string representation techniques : I see a lot of solutions mentioned, but I think most are looking at split. Split however does it at every occurrence of "." . What you would rather be looking for is partition.

    string = "folder/to_path/filename.ext"
    extension = string.rpartition(".")[-1]
    
    0 讨论(0)
  • 2020-11-22 14:11
    name_only=file_name[:filename.index(".")
    

    That will give you the file name up to the first ".", which would be the most common.

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