diff options
author | Guido van Rossum <guido@python.org> | 1991-01-01 18:17:49 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1991-01-01 18:17:49 (GMT) |
commit | 65a9620fa24d03d74c980403848faf1a47787d28 (patch) | |
tree | 6cec4eefb81b825fb5a2628c2feebca0db6a888e /Lib/glob.py | |
parent | 762c39e9d28ab901393b88f641fe5fc8447baea8 (diff) | |
download | cpython-65a9620fa24d03d74c980403848faf1a47787d28.zip cpython-65a9620fa24d03d74c980403848faf1a47787d28.tar.gz cpython-65a9620fa24d03d74c980403848faf1a47787d28.tar.bz2 |
Initial revision
Diffstat (limited to 'Lib/glob.py')
-rw-r--r-- | Lib/glob.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/Lib/glob.py b/Lib/glob.py new file mode 100644 index 0000000..8a70f0e --- /dev/null +++ b/Lib/glob.py @@ -0,0 +1,44 @@ +# Module 'glob' -- filename globbing. + +import posix +import path +import fnmatch + +def glob(pathname): + if not has_magic(pathname): return [pathname] + dirname, basename = path.split(pathname) + if dirname[-1:] = '/' and dirname <> '/': + dirname = dirname[:-1] + if has_magic(dirname): + list = glob(dirname) + else: + list = [dirname] + if not has_magic(basename): + result = [] + for dirname in list: + if basename or path.isdir(dirname): + name = path.cat(dirname, basename) + if path.exists(name): + result.append(name) + else: + result = [] + for dirname in list: + sublist = glob1(dirname, basename) + for name in sublist: + result.append(path.cat(dirname, name)) + return result + +def glob1(dirname, pattern): + if not dirname: dirname = '.' + try: + names = posix.listdir(dirname) + except posix.error: + return [] + result = [] + for name in names: + if name[0] <> '.' or pattern[0] = '.': + if fnmatch.fnmatch(name, pattern): result.append(name) + return result + +def has_magic(s): + return '*' in s or '?' in s or '[' in s |