Home > Enterprise >  Python Pathlib Strange Behaviour
Python Pathlib Strange Behaviour

Time:02-01

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?

CodePudding user response:

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.

CodePudding user response:

Regex style "solution":

from pathlib import Path
from re import search, IGNORECASE

folder_path = "."
images = r".*\.(jpg|jpeg|jfif|png)$"

files = list(filter(lambda f: search(images, str(f)), Path(folder_path).rglob("*")))

  • Related