fs.glob¶
Useful functions for working with glob patterns.
-
class
fs.glob.BoundGlobber(fs)[source]¶ A
Globberobject bound to a filesystem.An instance of this object is available on every Filesystem object as the
globproperty.-
__call__(pattern, path='/', namespaces=None, case_sensitive=True, exclude_dirs=None)[source]¶ Match resources on the bound filesystem againsts a glob pattern.
Parameters: - pattern (str) – A glob pattern, e.g.
"**/*.py" - namespaces (list) – A list of additional info namespaces.
- case_sensitive (bool) – If
True, the path matching will be case sensitive i.e."FOO.py"and"foo.py"will be different, otherwise path matching will be case insensitive. - exclude_dirs (list) – A list of patterns to exclude when searching,
e.g.
["*.git"].
Returns: An object that may be queried for the glob matches.
Return type: - pattern (str) – A glob pattern, e.g.
-
-
class
fs.glob.Counts(files, directories, data)¶ -
data¶ Alias for field number 2
-
directories¶ Alias for field number 1
-
files¶ Alias for field number 0
-
-
class
fs.glob.Globber(fs, pattern, path='/', namespaces=None, case_sensitive=True, exclude_dirs=None)[source]¶ A generator of glob results.
-
__init__(fs, pattern, path='/', namespaces=None, case_sensitive=True, exclude_dirs=None)[source]¶ Create a new Globber instance.
Parameters: - fs (FS) – A filesystem object
- pattern (str) – A glob pattern, e.g.
"**/*.py" - path (str) – A path to a directory in the filesystem.
- namespaces (list) – A list of additional info namespaces.
- case_sensitive (bool) – If
True, the path matching will be case sensitive i.e."FOO.py"and"foo.py"will be different, otherwise path matching will be case insensitive. - exclude_dirs (list) – A list of patterns to exclude when searching,
e.g.
["*.git"].
-
__iter__()[source]¶ Get an iterator of
fs.glob.GlobMatchobjects.
-
count()[source]¶ Count files / directories / data in matched paths.
Example
>>> my_fs.glob('**/*.py').count() Counts(files=2, directories=0, data=55)
Returns: A named tuple containing results. Return type: Counts
-
count_lines()[source]¶ Count the lines in the matched files.
Returns: A named tuple containing line counts. Return type: LineCountsExample
>>> my_fs.glob('**/*.py').count_lines() LineCounts(lines=4, non_blank=3)
-
-
class
fs.glob.LineCounts(lines, non_blank)¶ -
lines¶ Alias for field number 0
-
non_blank¶ Alias for field number 1
-
-
fs.glob.get_matcher(patterns, case_sensitive, accept_prefix=False)[source]¶ Get a callable that matches paths against the given patterns.
Parameters: - patterns (list) – A list of wildcard pattern. e.g.
["*.py", "*.pyc"] - case_sensitive (bool) – If
True, then the callable will be case sensitive, otherwise it will be case insensitive. - accept_prefix (bool) – If
True, the name is not required to match the patterns themselves but only need to be a prefix of a string that does.
Returns: a matcher that will return
Trueif the paths given as an argument matches any of the given patterns, or if no patterns exist.Return type: callable
Example
>>> from fs import glob >>> is_python = glob.get_matcher(['*.py'], True) >>> is_python('__init__.py') True >>> is_python('foo.txt') False
- patterns (list) – A list of wildcard pattern. e.g.
-
fs.glob.imatch(pattern, path)[source]¶ Compare a glob pattern with a path (case insensitive).
Parameters: Returns: Trueif the path matches the pattern.Return type:
-
fs.glob.imatch_any(patterns, path)[source]¶ Test if a path matches any of a list of patterns (case insensitive).
Will return
Trueifpatternsis an empty list.Parameters: Returns: Trueif the path matches at least one of the patterns.Return type:
-
fs.glob.match(pattern, path)[source]¶ Compare a glob pattern with a path (case sensitive).
Parameters: Returns: Trueif the path matches the pattern.Return type: Example
>>> from fs.glob import match >>> match("**/*.py", "/fs/glob.py") True