0

I tried using normal python regex patterns in pathlib glob but it's not working.
While reading online I came to know that the glob patterns are quite different.
I did not found the pattern online which I was looking for.

I have:
a folder named images like:

from pathlib import Path

print(list(Path('./images').glob("*")))

gives:

[WindowsPath('images/01.jpg'),
 WindowsPath('images/11.jpg'),
 WindowsPath('images/010.jpg'),
... ]

I want glob to extract only those images whose name starts with 0 like 01.jpg & 010.jpg & not 11.jpg
What will be the pattern to achieve this!

luckyCasualGuy
  • 641
  • 1
  • 5
  • 15

1 Answers1

1

Try using pathlib's iterdir and startswith

p = Path('./images')
for pth in p.iterdir():
    if pth.name.startswith('0'):
        print(pth)

images/01.jpg
images/010.jpg
Pygirl
  • 12,969
  • 5
  • 30
  • 43