Please note: This wiki is currently running in test mode after an attack on January 5 2013. All passwords were reset, so you will have to use the password recovery function to get a new password. To edit wiki pages, please log in first. See the wiki attack description page for more details. If you find problems, please report them to the pydotorg-www mailing list.


r""" hitlist.py

The glob module lists names in folders that match Unix shell patterns.

If the elemental function emulating Unix bash `echo *` really is missing
from the 2.5 Python batteries included, then here's a brief clear way of
adding that function to Python.

Mind you, to date I've never written an elemental function for Python that
I haven't eventually found well-buried somewhere, e.g., os.urandom.

See also: http://wiki.python.org/moin/glob
See also: http://wiki.python.org/moin/PathClass
"""

import glob

def hitlist(patterns, quiet = ''):

        """
        Choose files and folders like Unix bash ls and rm -f do.

        That is, visit each pattern. Substitute the sorted list of files
        and folder that match if any files or folders do match. Leave
        patterns that match no files or folders in place unchanged
        ordinarily, but delete those patterns if asked to be quiet.

        See glob in: Unix man bash | col -b
        """

        results = []

        for pattern in patterns:
                matches = glob.glob(pattern)
                if matches:
                        matches.sort()
                        results += matches
                elif not quiet:
                        results += [pattern]

        return results

if __name__ == '__main__':

        import sys
        args = sys.argv[1:]

        if not len(args):
                print "Usage: python hitlist.py [-f] 'pattern'"
        else:

                quiet = args[0]
                patterns = args[1:]
                if quiet != '-f':
                        quiet = ''
                        patterns = args
                print quiet, patterns

                hits = hitlist(patterns, quiet)
                for hit in hits:
                        print hit



CategoryFaq

glob (last edited 2008-11-15 13:59:46 by localhost)