Open In App

glob – Filename pattern matching

Glob module searches all path names looking for files matching a specified pattern according to the rules dictated by the Unix shell. Results so obtained are returned in arbitrary order. Some requirements need traversal 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 that match a given pattern under a specified folder.  

Pattern matching is done using os.scandir() and fnmatch.fnmatch() functions, and not by actually invoking a sub-shell. Unlike fnmatch.fnmatch(), glob treats filenames beginning with a dot (.) as special cases. For tilde and shell variable expansion, os.path.expanduser() and os.path.expandvars() functions are used.



Pattern rules 

Application

Functions in Glob:

Given below is the implementation to help you understand how this module can be put to practice:

Example 1:






import glob
  
# search .py files
# in the current working directory
for py in glob.glob("*.py"):
    print(py)

Output :

Example 2: Program to depict wildcard characters and ranges

If recursive is true, the pattern “**” will match any files and zero or more directories, subdirectories and symbolic links to directories. Using the “**” pattern in large directory trees may consume an inordinate amount of time. 




import glob
  
# Using character ranges []
print('Finding file using character ranges [] :- ')
print(glob.glob('./[0-9].*'))
  
# Using wildcard character *
print('\n Finding file using wildcard character * :- ')
print(glob.glob('*.gif'))
  
# Using wildcard character ?
print('\n Finding file using wildcard character ? :- ')
print(glob.glob('?.gif'))
  
# Using recursive attribute
print('\n Finding files using recursive attribute :- ')
print(glob.glob('**/*.txt', recursive=True))

Output :

Example 3:




import glob
  
gen = glob.iglob("*.py")
# returns class type of gen
type(gen)
  
for py in gen:
    print(py)

Output :

Example 4:




import glob
  
char_seq = "-_#"
  
for spcl_char in char_seq:
    esc_set = "*" + glob.escape(spcl_char) + "*" + ".py"
      
    for py in (glob.glob(esc_set)):
        print(py)

Output :


Article Tags :