Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 20, 2022 03:03 pm GMT

The Secrets of Python's Glob

Table of Common Glob Strings

GlobDefinitionExample GlobValid FilesNot Valid
*matches 0 or more characters*.jpg[im1.jpg,cat.jpg]cat.JPG, dog.png
?matches EXACTLY 1 character?_at.jpgbat.jpg,cat.jpghhat.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 \\ insteadimages=glob.glob("C:\\Python\\images\\*.jpg")# Returns #["C:\\Python\\images\\img.jpg","C:\\Python\\images\\img2.jpg"]

Full Code

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

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 \\ insteadimages=glob.glob1("C:\\Python\\images\\","*.jpg")# This also works:images=glob.glob1("C:\\Python\\images","*.jpg")# Returns #["img.jpg","img2.jpg"]

Full Code

import globimport 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"]

In Depth Glob Table

GlobDefinitionExample GlobValid FilesNot ValidExplaination
*matches 0 or more characters CASE SENSITIVE*.jpg[im1.jpg,cat.jpg]cat.JPG, dog.pngThe rest of the string must match
?matches EXACTLY 1 character?_at.jpgbat.jpg,cat.jpghhat.jpgOnly 1 character before the _at
**recursive search/*/.jpg[home/folders/img.jpg,folders/im.jpg]file.jpgCannot be without a parent directory
!()does not match characters in ()!(a)[rock.jpg,shell.txt]cat.txtA cannot be in the string
[]matches range of character in [] CASE SENSITIVE[ad]-file/*.txt[a-file.txt,d-file.txt]c-file.txtc 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.pngFiles with any name as long as they end in .jpg

Original Link: https://dev.to/2320sharon/the-secrets-of-glob-31a7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To