glob in Python
In this post, you will learn about the Python glob() method of the glob module. The glob is used to generate lists of files matching the given patterns according to the rules related to the Unix shell. The Python's glob module has several functions that can help in posting files under a predetermined folder. We may filter them dependent on extensions or with a specific string as a segment of the filename.
The pattern rules for glob are not regular expressions. They keep standard Unix way expansion rules. An asterisk (*) matches at least zero characters, and a question mark (?) matches precisely one character. You can likewise utilize brackets to show character ranges, for example, [0-9] for a solitary digit. Any remaining characters match themselves. Further, we have mentioned several examples that demonstrate this.
Import glob module
To utilize glob() and related functions, we need to import the glob module. This function returns a list of all files matching a given pattern.
import glob
Syntax of glob()
Here, the file_pattern is the absolute and relative path that can contain wild characters and the recursive parameter is false by default. If it is true, it recursively searches files under all sub-directories of the current directory.
glob(file_pattern, recursive = False)
Python search exact string with glob()
The following example demonstrates how to match the exact string or file name with an absolute path.
glob.glob('C:/python37/corpora/abc/science.txt')
Python glob with asterisk (*) wildcard
The asterisk (*) wildcard is used to match zero or more characters. It is generally used in glob() operations. The wildcard indicated that there might be zero character or numerous characters where the character isn't significant. In the given example, the code will match files those have .txt extension.
glob.glob("C:/python37/corpora/abc/*.txt")
Python glob with multilevel directories wildcards
We can also search in multi-level directories. If we are required to search one level down the directory for determining glob, we will use wildcards /*/ .
glob.glob("C:/python37/corpora/*/*.txt")
Python single character search with glob()
There is a question mark (?) wildcard which is utilized to match a single character. It matches any single character in that position in the name.
for name in glob.glob('C:/python37/scripts/projects/demo?.txt'):
print name
Python multiple character search with glob()
Glob additionally searches for alphabetic and numeric characters as well. We can utilize [ to begin character reach and ] to end character range. We can put anything we desire to match between square sections. In this example, we will match files and folders with those beginning with one of p, s, t.
glob.glob("C:/python37/scripts/projects/[pst]*.py")
Python search character range with glob()
At the point when you need to match a particular character, utilize a character range rather than a question mark (?). For instance, to find all of the files which have a digit in the name before the extension -
for name in glob.glob('C:/python37/scripts/projects/*[0-9].*'):
print(name)
Related Articles
zip function in Python
Python multiline string
Prettytable in Python
Multiply all elements in list Python
Count consonants in a string Python
Python dict inside list
Find the stop words in nltk Python
Python heapq
Python *args and **kwargs
Hollow Diamond Pattern in Python
Python program to sum all the numbers in a list
Difference between tuple and list in Python
Alphabetical order Python
find average of n numbers in Python
ASCII value in Python
Greatest common divisor (GCD) in Python
Python nonlocal keyword
Prime factor Python
casefold in Python
strip function in Python