Python Glossary Русский

all Python 3shutilfunction

shutil.ignore_patterns()

directory trees

Builds a filter function for copytree()'s ignore parameter: skip names that match glob-style patterns.

Syntax

shutil.ignore_patterns(*patterns)

Patterns are matched against names inside each directory, not against paths: '*.pyc' excludes such files at any depth, while a pattern with a slash like 'build/*.o' never matches. A matching directory is skipped entirely, with all its contents. ignore_patterns itself copies nothing — it only returns a function (directory, names) -> set of names to ignore.

Examples

import shutil
f = shutil.ignore_patterns('*.pyc', '__pycache__')
print(sorted(f('src', ['a.py', 'a.pyc', '__pycache__'])))  # → ['__pycache__', 'a.pyc']
import os, shutil
os.makedirs('src/pkg/__pycache__')
open('src/pkg/m.py', 'w').close()
open('src/pkg/m.pyc', 'w').close()
shutil.copytree('src', 'dst', ignore=shutil.ignore_patterns('*.pyc', '__pycache__'))
print(os.listdir('dst/pkg'))  # → ['m.py']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy