DEV Community

Cover image for The Secrets of Python's Glob
Sharon Fitzpatrick
Sharon Fitzpatrick

Posted on

The Secrets of Python's Glob

Table of Common Glob Strings

Glob Definition Example Glob Valid Files Not Valid
* matches 0 or more characters *.jpg [im1.jpg,cat.jpg] cat.JPG, dog.png
? matches EXACTLY 1 character ?_at.jpg bat.jpg,cat.jpg hhat.jpg
** recursive search /*/.jpg [home/imgs/1.jpg,imgs/im.jpg] file.jpg
!() does not match characters in () !(a) [rock.jpg,shell.txt] cat.txt
[] matches range of character in [] [ad]-img/*.txt [a-img.txt,d-img.txt] c-file.txt
*() matches 0 or more characters within the () "*(.jpg)" [file.jpg,blob.jpg,k.jpg] file.png

Interactive tool to test your glob

I discovered Glob Tool that lets you test out your glob strings with sample file paths. You type in the string glob you think would find the files you want then type the file path in the Test Strings box and it will show you if your file will be found. This is a great way to learn glob and saves your time from testing it on your computer.
Glob Tool Screenshot

Get the full path of all the jpegs in a folder

Get the full path of all the jpgs in a folder with glob.

TDLR

# Windows paths use \ so use \\ instead
images=glob.glob("C:\\Python\\images\\*.jpg")
# Returns 
#["C:\\Python\\images\\img.jpg","C:\\Python\\images\\img2.jpg"]
Enter fullscreen mode Exit fullscreen mode

Full Code

import glob
import os 
images_path=os.getcwd() + os.sep+"images"
# C:\\Python\\images\\
glob_str=images_path+"*jpg"
# C:\\Python\\images\\*jpg
full_images_paths=glob.glob(glob_str)
# On Windows Returns 
#["C:\\Python\\images\\img.jpg","C:\\Python\\images\\img2.jpg"]
Enter fullscreen mode Exit fullscreen mode

Get just the filenames in a folder

Get the just the names of all the jpgs in a folder with glob1. glob1 takes two arguments the file path you want to search and the glob string you pass in.
glob.glob1("file_path_to_search","pattern")

TDLR

# Windows paths use \ so use \\ instead
images=glob.glob1("C:\\Python\\images\\","*.jpg")
# This also works:
images=glob.glob1("C:\\Python\\images","*.jpg")
# Returns 
#["img.jpg","img2.jpg"]
Enter fullscreen mode Exit fullscreen mode

Full Code

import glob
import os 
images_path=os.getcwd() + os.sep+"images"
# C:\\Python\\images\\
full_images_paths=glob.glob(images_path,"*jpg")
# On Windows Returns 
#["img.jpg","img2.jpg"]
Enter fullscreen mode Exit fullscreen mode

In Depth Glob Table

Glob Definition Example Glob Valid Files Not Valid Explaination
* matches 0 or more characters CASE SENSITIVE *.jpg [im1.jpg,cat.jpg] cat.JPG, dog.png The rest of the string must match
? matches EXACTLY 1 character ?_at.jpg bat.jpg,cat.jpg hhat.jpg Only 1 character before the _at
** recursive search /*/.jpg [home/folders/img.jpg,folders/im.jpg] file.jpg Cannot be without a parent directory
!() does not match characters in () !(a) [rock.jpg,shell.txt] cat.txt A cannot be in the string
[] matches range of character in [] CASE SENSITIVE [ad]-file/*.txt [a-file.txt,d-file.txt] c-file.txt c is not in the [ad] so its not a match
*() matches 0 or more characters within the () "*(.jpg)" [file.jpg,blob.jpg,k.jpg] file.png Files with any name as long as they end in .jpg

Discussion (0)