0

glob.glob() does not use regex. it uses Unix path expansion rules. How can I emulate this regex in glob:

".*.jpg|.*.png"
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
alzoubi36
  • 33
  • 2
  • ```.*.{jpg,png}``` would be valid in a shell, but glob doesn't support that. But see this question for a workaround: [Brace expansion in python glob](https://stackoverflow.com/questions/22996645/brace-expansion-in-python-glob) – sj95126 Sep 23 '21 at 14:50

1 Answers1

4

Well, with glob you should just do this:

lst = glob.glob('*.jpg') + glob.glob('*.png')
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
  • thanks for the response. I am actually looking for a more elegant way. This will look like this if I use the pathlib module since its glob() method returns a generator object: lst = list(pathlib.Path.glob('*.jpg')) + list(pathlib.Path.glob('*.png')) – alzoubi36 Sep 23 '21 at 12:17
  • @alzoubi36 Yeah, that's the way, there isn't any more elegent solution unfortunately, feel free to mark and vote this if you like :) – U12-Forward Sep 23 '21 at 12:18
  • I voted up for the effort, but I won't mark it since I knew this solution. @U12-Forward – alzoubi36 Sep 23 '21 at 12:22
  • @alzoubi36 Well, this is the *only* one :) – U12-Forward Sep 23 '21 at 12:23