Search code examples
pythonglob

Why does glob('*[jp][pn]g') work but glob('*p[ng][df]') does not? (Syntax of glob with [])


from glob import glob

somefiles = glob(f'{samples_dir}/*p[ng][df]') 

Returns an empty list.

But this one returns all my png and jpg files. The folder contains png, jpg, and pdf files.

otherfiles = glob(f'{samples_dir}/*[jp][pn]g')

I'm just curious as to why the first one doesn't work. Clearly I don't fully understand the [] syntax of python3's glob method.


Solution

  • A [...] expression matches any single one of the characters contained between the braces. So:

    • [jp][pn]g would match "foo.jpg", "foo.jng", "foo.ppg", and "foo.png".
    • p[ng][df] would match "foo.pnd", "foo.pnf", "foo.pgd", and "foo.pgf"

    Most of the results for the second expression don't seem all that useful.

    For what you're doing, I would probably write something like:

    somefiles = (
      glob(f'{samples_dir}/*.png') +
      glob(f'{samples_dir}/*.jpg') +
      glob(f'{samples_dir}/*.pdf')
    )