Search code examples
pythonglobpathlib

Python Pathlib Strange Behaviour


I can find all the files in the subfolders of a folder path with specific filetypes in this way:

list(Path(folder_path).glob('**/*.[jpg][jpeg][png]*'))

But if I change the code to try and find other filetypes (like jfif or bmp), with some filetypes the code works and with the others, it can't find the filepaths:

list(Path(folder_path).glob('**/*.[jpg][jpeg][jfif][png]*'))

Why isn't this code working properly?


Solution

  • According to the pathlib docs, the pattern syntax is the same as for fnmatch. A pattern like [jpg] matches a single character, which is either a j, a p or a g. I don’t think this type of pattern can handle alternatives — you have to use some other mechanism for filtering; something like foo.suffix in [...] or a regexp.