Extracting extension from filename in Python

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

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

24条回答
  •  [愿得一人]
    2020-11-22 13:58

    For funsies... just collect the extensions in a dict, and track all of them in a folder. Then just pull the extensions you want.

    import os
    
    search = {}
    
    for f in os.listdir(os.getcwd()):
        fn, fe = os.path.splitext(f)
        try:
            search[fe].append(f)
        except:
            search[fe]=[f,]
    
    extensions = ('.png','.jpg')
    for ex in extensions:
        found = search.get(ex,'')
        if found:
            print(found)
    

提交回复
热议问题