The Secrets of Python's Glob
Tags: python, tutorial, glob
Published: 2022-04-20 15:03:37

## 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](https://www.digitalocean.com/community/tools/glob?comments=true&glob=%2F%2A%2Ajpg_files%2F%2A.txt&matches=false&tests=%2F%2F%20This%20will%20match%20as%20it%20ends%20with%20%27.js%27&tests=%2Fjpg_files%2Fworld.txt&tests=%2F%2F%20This%20won%27t%20match%21&tests=%2Ftest%2Fsome%2Fglobs) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zd0t4ssmwpc2qdzibew0.jpg)


# 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
```python
# Windows paths use \ so use \\ instead
images=glob.glob("C:\\Python\\images\\*.jpg")
# Returns 
#["C:\\Python\\images\\img.jpg","C:\\Python\\images\\img2.jpg"]
```
#### Full Code
```python
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"]
```

# Get just the filenames in a folder
Get the just the names of all the jpgs in a folder with [glob1](https://thomas-cokelaer.info/tutorials/python/module_glob.html#:~:text=5.3.%20methods,a%20pattern.%20So%3A). `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
```python
# 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"]
```
#### Full Code
```python
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"]
```


## 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 |