fs.glob

Useful functions for working with glob patterns.

class fs.glob.BoundGlobber(fs)[source]

A Globber object bound to a filesystem.

An instance of this object is available on every Filesystem object as the glob property.

__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:

Globber

__init__(fs)[source]

Create a new bound Globber.

Parameters:fs (FS) – A filesystem object to bind to.
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.GlobMatch(path, info)
info

Alias for field number 1

path

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.GlobMatch objects.

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:LineCounts

Example

>>> my_fs.glob('**/*.py').count_lines()
LineCounts(lines=4, non_blank=3)
remove()[source]

Remove all matched paths.

Returns:Number of file and directories removed.
Return type:int

Example

>>> my_fs.glob('**/*.pyc').remove()
2
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 True if 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
fs.glob.imatch(pattern, path)[source]

Compare a glob pattern with a path (case insensitive).

Parameters:
  • pattern (str) – A glob pattern.
  • path (str) – A path.
Returns:

True if the path matches the pattern.

Return type:

bool

fs.glob.imatch_any(patterns, path)[source]

Test if a path matches any of a list of patterns (case insensitive).

Will return True if patterns is an empty list.

Parameters:
  • patterns (list) – A list of wildcard pattern, e.g ["*.py", "*.pyc"]
  • path (str) – A resource path.
Returns:

True if the path matches at least one of the patterns.

Return type:

bool

fs.glob.match(pattern, path)[source]

Compare a glob pattern with a path (case sensitive).

Parameters:
  • pattern (str) – A glob pattern.
  • path (str) – A path.
Returns:

True if the path matches the pattern.

Return type:

bool

Example

>>> from fs.glob import match
>>> match("**/*.py", "/fs/glob.py")
True
fs.glob.match_any(patterns, path)[source]

Test if a path matches any of a list of patterns.

Will return True if patterns is an empty list.

Parameters:
  • patterns (list) – A list of wildcard pattern, e.g ["*.py", "*.pyc"]
  • path (str) – A resource path.
Returns:

True if the path matches at least one of the patterns.

Return type:

bool