Glob is a general term used to define techniques to match specified patterns according to rules related to Unix shell. Linux and Unix systems and shells also support glob and also provide function
glob() in system libraries.
In Python, the glob module is used to retrieve files/pathnames matching a specified pattern. The pattern rules of glob follow standard Unix path expansion rules. It is also predicted that according to benchmarks it is faster than other methods to match pathnames in directories. With glob, we can also use wildcards
("*, ?, [ranges]) apart from exact string search to make path retrieval more simple and convenient.
Note: This module comes built-in with Python, so there is no need to install it externally.
Using Glob() function to find files recursively
We can use the function
glob.iglob() directly from glob module to retrieve paths recursively from inside the directories/files and subdirectories/subfiles.
glob.glob(pathname, *, recursive=False)
glob.iglob(pathname, *, recursive=False)
Note: When recursive is set
**” followed by path separator
('./**/') will match any files or directories.
For older versions of python:
- Python | Create Archives and Find Files by Name
- Copy a directory recursively using Python (with examples)
- Working with PDF files in Python
- Working with csv files in Python
- Working with zip files in Python
- Writing CSV files in Python
- Reading CSV files in Python
- HDF5 files in Python
- Downloading files from web using Python
- Interact with files in Python
- Writing files in background in Python
- Python - Add audio files in kivy
- Rename multiple files using Python
- Python | Merge two text files
- Python | Reading .ini Configuration Files
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.