Python Glossary Русский

Python 3.4+globfunction

glob.escape()

file search

Escapes the special characters *, ? and [ in a path so that a file with such a name is matched literally.

Syntax

glob.escape(pathname) -> str

A name like 'report[1].txt' is treated as a pattern for 'report1.txt' unless escaped, so the file itself is not found. escape() wraps each special character in brackets. Escape only the variable part, not the whole pattern: glob.escape(folder) + '/*.txt'.

Examples

import glob
open('report[1].txt', 'w').close()
print(glob.glob('report[1].txt'))  # → []
print(glob.glob(glob.escape('report[1].txt')))  # → ['report[1].txt']
print(glob.escape('a*?.txt'))  # → a[*][?].txt

See also

Open in the full glossarydocs.python.orgReport an inaccuracy