source: vendor/python/2.5/Lib/glob.py@ 3298

Last change on this file since 3298 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 2.0 KB
RevLine 
[3225]1"""Filename globbing utility."""
2
3import os
4import fnmatch
5import re
6
7__all__ = ["glob", "iglob"]
8
9def glob(pathname):
10 """Return a list of paths matching a pathname pattern.
11
12 The pattern may contain simple shell-style wildcards a la fnmatch.
13
14 """
15 return list(iglob(pathname))
16
17def iglob(pathname):
18 """Return a list of paths matching a pathname pattern.
19
20 The pattern may contain simple shell-style wildcards a la fnmatch.
21
22 """
23 if not has_magic(pathname):
24 if os.path.lexists(pathname):
25 yield pathname
26 return
27 dirname, basename = os.path.split(pathname)
28 if not dirname:
29 for name in glob1(os.curdir, basename):
30 yield name
31 return
32 if has_magic(dirname):
33 dirs = iglob(dirname)
34 else:
35 dirs = [dirname]
36 if has_magic(basename):
37 glob_in_dir = glob1
38 else:
39 glob_in_dir = glob0
40 for dirname in dirs:
41 for name in glob_in_dir(dirname, basename):
42 yield os.path.join(dirname, name)
43
44# These 2 helper functions non-recursively glob inside a literal directory.
45# They return a list of basenames. `glob1` accepts a pattern while `glob0`
46# takes a literal basename (so it only has to check for its existence).
47
48def glob1(dirname, pattern):