DEV Community

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

Posted on

3 2

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay