pythonglobpathlib

How to work with regex in Pathlib correctly?


I want find all images and trying to use pathlib, but my reg expression don't work. where I went wrong?

from pathlib import Path
FILE_PATHS=list(Path('./photos/test').rglob('*.(jpe?g|png)'))
print(len(FILE_PATHS))
FILE_PATHS=list(Path('./photos/test').rglob('*.jpg'))#11104
print(len(FILE_PATHS))

0
11104

Solution

  • Get list of files using Regex

    import re
    p = Path('C:/Users/user/Pictures')
    files = []
    for x in p.iterdir(): 
        a = re.search('.*(jpe?g|png)',str(x))
        if a is not None:
            files.append(a.group())