Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on

     

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 discoveredGlob 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 theTest 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"]
Enter fullscreen modeExit fullscreen mode

Full Code

importglobimportosimages_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"]
Enter fullscreen modeExit fullscreen mode

Get just the filenames in a folder

Get the just the names of all the jpgs in a folder withglob1.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"]
Enter fullscreen modeExit fullscreen mode

Full Code

importglobimportosimages_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 modeExit fullscreen mode

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

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Open Source Software Developer who loves Python
  • Location
    Roseville, Ca
  • Education
    California State University, Sacramento
  • Work
    software developer
  • Joined

More fromSharon Batiste

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp