globmatch
globmatch provides functions for matching a path against one or more glob patterns in Python. Unlike the standard library's `glob` module, it does not interact with the filesystem, focusing solely on generic string matching. It extends `fnmatch` capabilities by supporting the `**` pattern for matching zero or more directories. The current version is 2.0.0, with releases historically being infrequent but demonstrating active maintenance.
Common errors
-
glob_match('path/to/file', ['path/*/file']) returns False when it should match 'path/subdir/file'.cause The `*` wildcard only matches within a single path component. It does not cross directory boundaries.fixUse the `**` wildcard for recursive matching across directories. Change the pattern to `['path/**/file']`. -
My patterns work in bash/zsh but globmatch doesn't find files.
cause The `globmatch` library *only* matches strings against patterns; it does not list or interact with files on the filesystem. This is a common confusion with shell globbing or Python's `glob` module.fixIf your goal is to find actual files on the filesystem, use Python's built-in `glob.glob()` or `pathlib.Path.glob()` functions. `globmatch` is for abstract path string matching. -
`glob_match('foo/.config', ['.config'])` returns `False` or inconsistent results on Windows.cause Older versions of `globmatch` (prior to 2.0.0) had known issues with `**` patterns on Windows and inconsistent matching of dotfiles.fixEnsure you are using `globmatch` version `2.0.0` or newer. This version specifically addressed these compatibility and matching issues.
Warnings
- breaking Version 2.0.0 introduced fixes for the `**` pattern on Windows and for matching dotfiles. Code relying on previous, potentially incorrect, behavior in these areas may break.
- gotcha The `*` wildcard in `globmatch` matches zero or more characters within a single path segment only (i.e., it does not cross path separators like `/` or `\`). For matching across zero or more directories recursively, you must explicitly use the `**` wildcard.
- gotcha `globmatch` is purely a string pattern matching library and does not interact with the filesystem. It cannot be used to find or list actual files on disk. Its purpose is to test if a given string path matches a glob pattern.
Install
-
pip install globmatch
Imports
- glob_match
from globmatch import glob_match
Quickstart
from globmatch import glob_match
# Basic matching
print(glob_match('foo/config', ['**/config'])) # Expected: True
print(glob_match('foo/bar/config', ['**/config'])) # Expected: True
print(glob_match('foo/config/bar', ['foo/config'])) # Expected: False (needs /** for sub-elements)
# Matching dotfiles
print(glob_match('.git/gitconfig', ['.git/**'])) # Expected: True
# Multiple patterns
patterns = ['!.git', '**/config'] # Match config, but not if it's in .git
print(glob_match('.git/config', patterns, match_all=True)) # Expected: False
print(glob_match('myproject/config', patterns, match_all=True)) # Expected: True