pythonglob

How to write "or" in a glob() pattern?


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

".*.jpg|.*.png"

Solution

  • @U12-Forward is correct that there isn't an exact solution but depending on your use case you might be able to solve it with the [...] wildcard. For your example with .png or .jpg you could use this:

    .*.[jp]*
    

    which will match any extension that starts with a j or p

    If you have other extensions that start with j or p you can be more specific:

    .*.[jp][pn]g