Skip to content

[Python] Using Python module “glob” to find file with UNIX rule

“glob” is a Python built-in module, it can allow us to use a UNIX rule to find our target file. After I tested, it worked normally in Windows OS. It’s convenience if you similar to use Linux terminal.

In the following notes, I will note some usages of “glob”. If I find there are more frequently used functions, I would like to organize in this article.

You can also refer the document of glob module: https://docs.python.org/3/library/glob.html


Instructions of glob

I created a folder named “test_folder”, and put in some files and they have different names.

# -*- coding: utf-8 -*-
import os
import glob

print(os.listdir('test_folder'))



Output:

['0.txt', '1.txt', '3.jpg', '4.jpg', '5.png']

We can show all files in our screen first by function “os.listdir()”. And then we using moduleglob to try again.

print(glob.glob('./*/*'))



Output:

['.\test_folder\0.txt', '.\test_folder\1.txt', '.\test_folder\3.jpg', '.\test_folder\4.jpg', '.\test_folder\5.png']

Our rule is find all files in every folders, but we just only have one floder “test_folder”, so we only print the files in “test_folder”.

We can only match “txt” file.

print(glob.glob('./*/*txt'))



Output:

['.\test_folder\0.txt', '.\test_folder\1.txt']

In addition, if we don’t want to show the path of files we match, we can use “glob.iglob()” to return the iterable object.

print(glob.iglob('*/*.jpg'))
for file in glob.iglob('*/*.jpg'):
    print(file)



Output:

<generator object _iglob at 0x000001935F68FD58>
test_folder\3.jpg
test_folder\4.jpg

Another common problems is about escape characters (for example: “?”, “*”)
We can use “glob.escape()” to generate the match rule automatically.

print(glob.escape('./*/*.jpg'))



Output:

./[]/[].jpg

Tags:

Leave a Reply