5

I have a folder containing files in different types. I only need to deal with image files (jpg, png, jpeg, JPG, ...). For that, I use glob.glob to get only files with 'jpg', 'png', ... extensions.
The problem is that I have to use glob.glob many times (as much as the image extensions number) to get all the images:

import glob
images = glob.glob('tests/*.jpg') + glob.glob('tests/*.jpeg') + glob.glob('tests/*.png') + glob.glob('tests/*.JPG')  

Is there a way to use glob.glob in a more optimized form (something like glob.glob('path/*.{'jpg', 'png', 'jpeg', 'JPG'}) or is there a simpler function to use it instead of glob?

singrium
  • 2,746
  • 5
  • 32
  • 45

2 Answers2

4

Using pathlib:

from pathlib import Path

extensions = ['.jpg', '.png', '.jpeg']
images = [x for x in Path('tests').iterdir() if x.suffix.lower() in extensions]
Alex
  • 6,610
  • 3
  • 20
  • 38
  • When I run the code you suggested, I got this output: `[PosixPath('tests/a.jpg'), PosixPath('tests/b.jpg'), PosixPath('tests/d.png'), PosixPath('tests/e.jpeg'), PosixPath('tests/c.JPG')] `, what `PosixPath()` stands for? – singrium Feb 12 '19 at 15:57
  • 1
    @singrium what about reading the FineManual ? https://docs.python.org/3/library/pathlib.html – bruno desthuilliers Feb 12 '19 at 15:58
  • 1
    These are described here: https://docs.python.org/3/library/pathlib.html#pure-paths. They are a platform independent way of representing files. – Alex Feb 12 '19 at 15:59
  • 1
    @brunodesthuilliers, I'll take a look at the documentation. Thank you. – singrium Feb 12 '19 at 16:01
3

You cannot do complex wildcard globbing like glob.glob('*.{JPG, png..}) because, if you take a look at the source code you will see,

def glob(pathname):
    """Return a list of paths matching a pathname pattern.
    ....
    """
    return list(iglob(pathname))

And then if you find the source of iglob you will then see,

def iglob(pathname):
    ....
    ....
    dirname, basename = os.path.split(pathname)

    # voila, here, our complex glob wildcard will certainly break, and can't be used :)

Therefore, you can only do simple globbing using glob :)

han solo
  • 6,390
  • 1
  • 15
  • 19