glob in Python
In this post, let's 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.
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 absolute path.
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 character where character isn't significant. In the given example, the code will match files those have .txt extension.
Python glob with multilevel directories wildcards
We can also search in multi level directories. If we required to search one level down directory for determining glob we will use wildcards /*/ .
Python single character search with glob()
There is a question mark (?) wildcard which is utilized to match 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 ] is utilized to end character range. We can put anything we desire to match between square sections. In this example, we will match files and folders names those beginnings one of p,s,t.
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)
range and xrange in Python
2d arrays in Python
splitlines in python
Simple Calculator Program in Python
strip function in Python
casefold in Python
Prime factors of a number in Python
Python nonlocal keyword
Greatest common divisor Python recursive
Python String isalpha() Method
Program to print ASCII Value of a character
Python program to sort words in alphabetical order
*args and **kwargs in Python
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module