Skip to main content

Version of the glob module that can capture patterns and supports recursive wildcards

Project description

This is an extended version of Python’s builtin glob module (http://docs.python.org/library/glob.html) which adds:

  • The ability to capture the text matched by glob patterns, and return those matches alongside the filenames.

  • A recursive ‘**’ globbing syntax, akin for example to the globstar option of the bash shell.

  • The ability to replace the filesystem functions used, in order to glob on virtual filesystems.

  • Compatible with Python 2 and Python 3 (tested with 3.3).

It’s currently based on the glob code from Python 3.3.1.

Examples

Matches being returned:

import glob2

for filename, (version,) in glob2.iglob('./binaries/project-*.zip', with_matches=True):
    print version

Recursive glob:

>>> import glob2
>>> all_header_files = glob2.glob('src/**/*.h')
['src/fs.h', 'src/media/mp3.h', 'src/media/mp3/frame.h', ...]

Note that ** must appear on it’s own as a directory element to have its special meaning. **h will not have the desired effect.

** will match “.”, so **/*.py returns Python files in the current directory. If this is not wanted, */**/*.py should be used instead.

Custom Globber:

from glob2 import Globber

class VirtualStorageGlobber(Globber):
    def __init__(self, storage):
        self.storage = storage
    def listdir(self, path):
        # Must raise os.error if path is not a directory
        return self.storage.listdir(path)
    def exists(self, path):
        return self.storage.exists(path)
    def isdir(self, path):
        # Used only for trailing slash syntax (``foo/``).
        return self.storage.isdir(path)
    def islink(self, path):
        # Used only for recursive glob (``**``).
        return self.storage.islink(path)

globber = VirtualStorageGlobber(sftp_storage)
globber.glob('/var/www/**/*.js')

If isdir and/or islink cannot be implemented for a storage, you can make them return a fixed value, with the following consequences:

  • If isdir returns True, a glob expression ending with a slash will return all items, even non-directories, if it returns False, the same glob expression will return nothing.

  • Return islink True, the recursive globbing syntax ** will follow all links. If you return False, it will not work at all.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

glob2-0.5.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

glob2-0.5-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file glob2-0.5.tar.gz.

File metadata

  • Download URL: glob2-0.5.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for glob2-0.5.tar.gz
Algorithm Hash digest
SHA256 8f944b87ac4deaf39b0dce419665b40a3acb66a9ad3b07945e109246bb66fe9f
MD5 53fce1ef8ac8d38c7852c09003d325c0
BLAKE2b-256 53aea8b28dfc011b7c645bd6aa4591ffa14db9fb48f08a32034374e01ca75ff3

See more details on using hashes here.

File details

Details for the file glob2-0.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for glob2-0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 58c64a566a84a4ffed98e3bc2346f9d5d761df6203bf7b1c854d4c2edd332bc9
MD5 8cb7c39fe33ed6d0ebf069f12079dd4a
BLAKE2b-256 6aa88a45e999b56b00e7a0895679359ebd311c595e6553e529c48b9900fa2d07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page