Python glob Module
Example
Find files matching a pattern:
import glob
import tempfile
import os
with tempfile.TemporaryDirectory() as d:
open(os.path.join(d, "Emil.txt"), "w").close()
open(os.path.join(d, "Linus.txt"), "w").close()
open(os.path.join(d, "Tobias.py"), "w").close()
hits = glob.glob(os.path.join(d, "*.txt"))
print(sorted(os.path.basename(x) for x in hits))
Try it Yourself »
Definition and Usage
The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell.
Use it to list files matching wildcards like *.py, data-??.csv inside directories.
Members
Member | Description |
---|---|
escape() | Escape all special characters. |
glob() | Return a list of paths matching a pathname pattern. |
iglob() | Return an iterator yielding pathnames matching a pattern. |