Thursday, January 23, 2020

Glob module in python


Usually, the programmers require to traverse through a list of files at some location, mostly having a specific pattern. Python’s glob module 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 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

There are three primary functions of Glob module.

Glob(File_pattern, Recursive = False)

It retrieves the list of files matching the specified 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 turn 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 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 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 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.

No comments:

Post a Comment