Guido van Rossum | ab096c9 | 1997-04-02 05:47:11 +0000 | [diff] [blame] | 1 | """Filename globbing utility.""" |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | bba77af | 1992-01-12 23:26:24 +0000 | [diff] [blame] | 3 | import os |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 4 | import fnmatch |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 5 | import re |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 6 | |
Skip Montanaro | eccd02a | 2001-01-20 23:34:12 +0000 | [diff] [blame^] | 7 | __all__ = ["glob"] |
Guido van Rossum | bba77af | 1992-01-12 23:26:24 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 9 | def glob(pathname): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 10 | """Return a list of paths matching a pathname pattern. |
Guido van Rossum | ab096c9 | 1997-04-02 05:47:11 +0000 | [diff] [blame] | 11 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 12 | The pattern may contain simple shell-style wildcards a la fnmatch. |
Guido van Rossum | ab096c9 | 1997-04-02 05:47:11 +0000 | [diff] [blame] | 13 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 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 has_magic(dirname): |
| 22 | list = glob(dirname) |
| 23 | else: |
| 24 | list = [dirname] |
| 25 | if not has_magic(basename): |
| 26 | result = [] |
| 27 | for dirname in list: |
| 28 | if basename or os.path.isdir(dirname): |
| 29 | name = os.path.join(dirname, basename) |
| 30 | if os.path.exists(name): |
| 31 | result.append(name) |
| 32 | else: |
| 33 | result = [] |
| 34 | for dirname in list: |
| 35 | sublist = glob1(dirname, basename) |
| 36 | for name in sublist: |
| 37 | result.append(os.path.join(dirname, name)) |
| 38 | return result |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 39 | |
| 40 | def glob1(dirname, pattern): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 41 | if not dirname: dirname = os.curdir |
| 42 | try: |
| 43 | names = os.listdir(dirname) |
| 44 | except os.error: |
| 45 | return [] |
| 46 | result = [] |
| 47 | for name in names: |
| 48 | if name[0] != '.' or pattern[0] == '.': |
| 49 | if fnmatch.fnmatch(name, pattern): |
| 50 | result.append(name) |
| 51 | return result |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 54 | magic_check = re.compile('[*?[]') |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 56 | def has_magic(s): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 57 | return magic_check.search(s) is not None |