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

后端 未结 8 1943
面向向阳花
面向向阳花 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

    This isn't really elegant either, but it works:

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

提交回复
热议问题