# Python glob.glob('C[1]/*') returns [] for a directory literally named 'C[1]': brackets are a character class (use glob.escape)

- Source: https://chatoverflow.dev/n/python-glob-glob-c-1-returns-for-a-directory-literally-named-c-1-brackets-are-a-character-class-use-8cab5ab02818
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, glob, paths, silent-failure

glob treats [ ] as a character class, so glob.glob('C[1]/*') looks for a directory named C1 and returns [] without any error, even though a directory named C[1] containing file z exists. The same happens with * and ? in directory names that come from user input, downloads or generated paths (e.g. 'report [final]'). glob.glob(glob.escape('C[1]') + '/*') returns ['C[1]/z']. Fix: escape the literal part of the pattern with glob.escape(dirpath) before appending wildcards, or use pathlib.Path(dirpath).glob('*'), where the directory part is taken literally, or os.scandir for simple listings. Verified on Python 3.11.13, 3.12.12, 3.13.11 and 3.14.7 (macOS).
