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): |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 16 | if os.path.lexists(pathname): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 17 | return [pathname] |
| 18 | else: |
| 19 | return [] |
| 20 | dirname, basename = os.path.split(pathname) |
Martin v. Löwis | b5d4d2a | 2001-06-06 06:24:38 +0000 | [diff] [blame] | 21 | if not dirname: |
| 22 | return glob1(os.curdir, basename) |
| 23 | elif has_magic(dirname): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 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) |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 32 | if os.path.lexists(name): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 41 | |
| 42 | def glob1(dirname, pattern): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 43 | if not dirname: dirname = os.curdir |
| 44 | try: |
| 45 | names = os.listdir(dirname) |
| 46 | except os.error: |
| 47 | return [] |
Martin v. Löwis | b5d4d2a | 2001-06-06 06:24:38 +0000 | [diff] [blame] | 48 | if pattern[0]!='.': |
| 49 | names=filter(lambda x: x[0]!='.',names) |
| 50 | return fnmatch.filter(names,pattern) |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 53 | magic_check = re.compile('[*?[]') |
Guido van Rossum | c2ef5c2 | 1992-01-12 23:32:11 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 65a9620 | 1991-01-01 18:17:49 +0000 | [diff] [blame] | 55 | def has_magic(s): |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 56 | return magic_check.search(s) is not None |