Python glob() Method Tutorial

Python provides the glob module in order to search and find pathnames for specified search terms or search patterns. As the glob operators * ? [] are used in the Unix style operating systems like Linux, Ubuntu, CentOS, BSD the glob is also called as Unix style pathname pattern expansion. In this tutorial, we will examine the glob module glob() method which can be also expressed as glob.glob().

glob() Method Syntax

The glob() method is provided via glob module. In order to use glob() method we should import glob module. The glob method has the following syntax.

glob.glob(PATHNAME,*,RECURSIVE)
  • PATHNAME is the path and file name we want to search and match for. Generally full or absolute path is provided with the file or directory name we want to match. This parameter is required.
  • RECURSIVE is used to enabled recursive search. The RECURSIVE is false by default which means recursive search is not enabled by default. This parameter is optional.

* Sign - Match Everything

The * or asterisk sign is used to match none, single or more characters. It is very flexible to express names. In the following example we will list evey file and directory under the "/home/ismail" path.

import glob

for name in glob.glob('/home/ismail/*'): 
   print(name)

We can see from the output that every file and folder is matched.

/home/ismail/userinput.py
 /home/ismail/ipaddress.txt
 /home/ismail/Pictures
 /home/ismail/data2
 /home/ismail/random.c
 /home/ismail/file1
 /home/ismail/uniqip.txt
 /home/ismail/config.ttxt
 /home/ismail/names.txt
 /home/ismail/data4
 /home/ismail/mylink
 /home/ismail/file3
 /home/ismail/while.sh
 /home/ismail/Desktop
 /home/ismail/Videos
...

Alternative we can use the * asterisk sign in order to match or glob specified file extensions. For example the "*.txt" can be used to match and list only text files.

import glob

for name in glob.glob('/home/ismail/*.txt'): 
   print(name)

The output is like below where only text files with "*.txt" are listed.

/home/ismail/ipaddress.txt
 /home/ismail/uniqip.txt
 /home/ismail/names.txt
 /home/ismail/sample.txt
 /home/ismail/IPaddress_sorted.txt
 /home/ismail/file.txt
 /home/ismail/text.txt

Another popular and useful use case for the * asterisk character is providing some part of the file or folder name and filter all. In the following example we will match files and directories those names contain "data".

import glob

for name in glob.glob('/home/ismail/*data*'): 
   print(name)

The output is like below.

/home/ismail/data2
/home/ismail/data4
/home/ismail/data5
/home/ismail/data1
/home/ismail/data3

? Sign - Match Any Single Character

The ? or question mark is used to specify a single character. This single character can be anything like a letter or number. In the following example, we will search for "data?" where the last character can be anything.

import glob

for name in glob.glob('/home/ismail/data?'): 
   print(name)

The output is like below.

/home/ismail/data2
/home/ismail/data4
/home/ismail/data5
/home/ismail/data1
/home/ismail/data3

[] - Match Specified Character Range

The [] brackets are used to specify multipe characters or character range. For example [0-9] specifies the numbers from 0 to 9.

import glob

for name in glob.glob('/home/ismail/*[0-9]*'): 
   print(name)

The output is like below.any file or directory name that contains at least single number is listed.

/home/ismail/data2
/home/ismail/file1
/home/ismail/data4
/home/ismail/file3
/home/ismail/data5
/home/ismail/nmap-7.91.tar.bz2
/home/ismail/data1
/home/ismail/data3
/home/ismail/file4
/home/ismail/file2

Use Glob Find Files Recursively In Child Directories

Another useful feature of the glob() method is listing the directories recursively. This means the provided path and its child directories are searched for the specified file name. For the path, the ** double asterisk is used to specify child directories and the recursive parameter set to True.

import glob

for name in glob.glob('/home/ismail/**/*.txt',recursive=True): 
   print(name)

The output is like below.

/home/ismail/ipaddress.txt
/home/ismail/uniqip.txt
/home/ismail/names.txt
/home/ismail/sample.txt
/home/ismail/IPaddress_sorted.txt
/home/ismail/file.txt
/home/ismail/text.txt
/home/ismail/Desktop/usomlist.txt
/home/ismail/Downloads/myfile.txt
/home/ismail/Templates/Plain Text.txt

Leave a Comment