Python Glossary Русский

all Python 3globfunction

glob.glob()

file search

Returns a list of paths matching a shell-style pattern: *, ? and [abc], plus ** with recursive=True.

Syntax

glob.glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False) -> list[str]

The order of results is arbitrary and depends on the file system, so use sorted() for stable output. ** is recursive only with recursive=True; otherwise it acts like *. Files starting with a dot are not matched by * (as in a shell), and a missing directory yields an empty list rather than an error.

Examples

import glob, os
os.makedirs('src/sub', exist_ok=True)
for name in ['a.py', 'b.txt', 'sub/c.py', '.hidden.py']:
    open(os.path.join('src', name), 'w').close()
print(sorted(glob.glob('*.py', root_dir='src')))  # → ['a.py']
print(sorted(glob.glob('**/*.py', root_dir='src', recursive=True)))  # → ['a.py', 'sub/c.py']
print(glob.glob('nope/*'))  # → []

See also

Open in the full glossarydocs.python.orgReport an inaccuracy