Python .glob()

BrandonDusch's avatar
Published Jun 25, 2022
Contribute to Docs

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

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.

Example

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

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

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours