Python Glob Module #
glob
– useful when working with multiples files. Stores files as a list.
Module #
import glob
Grab files with specified ending #
files = glob.glob("<directory>/*.<file ending>")
files
would be a list.
Once this is done, something like this is viable:
for file in files:
print(file) # this is silly
Or read them line by line:
for file in files:
with open(file, 'r') as f: # context manager
lines = f.readlines()
for line in lines:
print(line)
Recursive glob #
To search for a particular filename pattern recursively within a directory:
files = glob.glob("<directory>/**/<file pattern>", recursive=True)