Use fnmatch.filter to filter files by more than one possible file extension

后端 未结 8 1921
面向向阳花
面向向阳花 2021-01-31 02:52

Given the following piece of python code:

for root, dirs, files in os.walk(directory):
    for filename in fnmatch.filter(files, \'*.png\'):
        pass
         


        
8条回答
  •  爱一瞬间的悲伤
    2021-01-31 03:06

    If you only need to check extensions (i.e. no further wildcards), why don't you simply use basic string operations?

    for root, dirs, files in os.walk(directory):
        for filename in files:
            if filename.endswith(('.jpg', '.jpeg', '.gif', '.png')):
                pass
    

提交回复
热议问题