The .glob() method returns a list of possible matches for a pathname string.


glob.glob(pathname, recursive)

The pathname must be a valid string that represents a file path that can either be absolute or relative.

By default, recursive is set to False. But when set to True, the pattern ** can be used to match any files and folders in a given directory.


The example below showcases some common use-cases for the .glob() method:

import glob
# Files in current folder
# Files in subfolder
# Everything in current folder
glob.glob('**/*', recursive=True)


Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Python on Codecademy