Extracting extension from filename in Python

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

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

24条回答
  •  盖世英雄少女心
    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 ""
    

提交回复
热议问题