This commit was manufactured by cvs2svn to create tag 'r23b1-mac'.
[python/dscho.git] / Lib / glob.py
blobd5e508abab9d27d2a5707d4b93673eb4d215952d
1 """Filename globbing utility."""
3 import os
4 import fnmatch
5 import re
7 __all__ = ["glob"]
9 def glob(pathname):
10 """Return a list of paths matching a pathname pattern.
12 The pattern may contain simple shell-style wildcards a la fnmatch.
14 """
15 if not has_magic(pathname):
16 if os.path.exists(pathname):
17 return [pathname]
18 else:
19 return []
20 dirname, basename = os.path.split(pathname)
21 if not dirname:
22 return glob1(os.curdir, basename)
23 elif has_magic(dirname):
24 list = glob(dirname)
25 else:
26 list = [dirname]
27 if not has_magic(basename):
28 result = []
29 for dirname in list:
30 if basename or os.path.isdir(dirname):
31 name = os.path.join(dirname, basename)
32 if os.path.exists(name):
33 result.append(name)
34 else:
35 result = []
36 for dirname in list:
37 sublist = glob1(dirname, basename)
38 for name in sublist:
39 result.append(os.path.join(dirname, name))
40 return result
42 def glob1(dirname, pattern):
43 if not dirname: dirname = os.curdir
44 try:
45 names = os.listdir(dirname)
46 except os.error:
47 return []
48 if pattern[0]!='.':
49 names=filter(lambda x: x[0]!='.',names)
50 return fnmatch.filter(names,pattern)
53 magic_check = re.compile('[*?[]')
55 def has_magic(s):
56 return magic_check.search(s) is not None