This tutorial provides an overview of the Python glob() method of the glob module. It includes several examples to bring clarity.
Introduction
Usually, the programmers are required to traverse through a list of files at some location, mostly having a specific pattern.
The glob module in Python has several functions that can help in listing files under a specified folder. We may filter them based on extensions or with a particular string as a portion of the filename.
All the methods of the Glob module follow the Unix-style pattern-matching mechanism and rules. However, it doesn’t allow expanding the tilde (~) and environment variables.
Python Glob() Methods
Today, we are going to discuss three primary functions of the Glob module.
glob(file_pattern, recursive = False)
It retrieves the list of files matching the specified Python pattern in the file_pattern parameter.
The file_pattern can be an absolute or relative path. It may also contain wild cards such as “*” or “?” symbols.
The recursive parameter is turned off (False) by default. When True, it recursively searches files under all subdirectories of the current directory.
Let’s now check out some examples:
Check the current directory for Python script files
The below code checks for .py files in the current dir only.
>>> import glob >>> for py in glob.glob("*.py"): ... print(py) ... copy_file1.py copy_file2.py decimal_sample.py
Another sample code – It checks for .py files in the current dir and subdirectories.
>>> import glob >>> for py in glob.glob("*.py"): ... print(py) ... copy_file1.py copy_file2.py decimal_sample.py test_num.py test_python_in_with_if.py test_scope.py
List files with a pattern
We can provide a pathname pattern by including some wild cards like? or numeric range [0-9]. The below code lists all files whose name starts with “test” followed by a number.
>>> for py in glob.glob("test[0-9].py"): ... print(py) ... test1.py test2.py
Let’s check one more example using the question mark in the pattern.
>>> for py in glob.glob("?????.py"): ... print(py) ... quiz1.py test1.py test2.py
The above Python for loop statement printed all .py files having five letters.
The following statement would print the names of folders recursively in the current working directory.
>>> glob.glob('selenium/**/', recursive=True) ['selenium\\', 'selenium\\webdriver\\', 'selenium\\webdriver\\firefox\\', 'selen ium\\webdriver\\firefox\\amd64\\', 'selenium\\webdriver\\firefox\\x86\\']
iglob() method | Python Glob
This method creates a Python generator object which can be used to list files under a given directory. You can call the next() function to print the names of files.
Check the sample code below:
>>> gen = glob.iglob("*.py") >>> type(gen) <class 'generator'> >>> for py in gen: ... print(py) ... copy_file1.py copy_file2.py decimal_sample.py find_seed.py frozen_set.py
escape() method
It allows for escaping the given character sequence. You can find it handy for locating files with certain characters in their file names.
Check out the below examples:
>>> char_seq = "-_#" >>> for spcl_char in char_seq: ... esc_set = "*" + glob.escape(spcl_char) + "*" + ".py" ... for py in (glob.glob(esc_set)): ... print(py) ... python quiz-classes-2.py python-class.py python-lists.py python-random#num.py python-set.py python-tuples.py python-while_loop.py copy_file#2.py decimal_sample.py find_seed.py frozen-set.py
Must check out – Python to list all files in a directory