Use module 'os'

This commit is contained in:
Guido van Rossum 1992-01-12 23:26:24 +00:00
parent 2817875b2e
commit bba77af37a

View File

@ -1,14 +1,12 @@
# Module 'glob' -- filename globbing. # Module 'glob' -- filename globbing.
import posix import os
import path
import fnmatch import fnmatch
def glob(pathname): def glob(pathname):
if not has_magic(pathname): return [pathname] if not has_magic(pathname): return [pathname]
dirname, basename = path.split(pathname) dirname, basename = os.path.split(pathname)
if dirname[-1:] == '/' and dirname <> '/':
dirname = dirname[:-1]
if has_magic(dirname): if has_magic(dirname):
list = glob(dirname) list = glob(dirname)
else: else:
@ -16,25 +14,24 @@ def glob(pathname):
if not has_magic(basename): if not has_magic(basename):
result = [] result = []
for dirname in list: for dirname in list:
if basename or path.isdir(dirname): if basename or os.path.isdir(dirname):
name = path.join(dirname, basename) name = os.path.join(dirname, basename)
if path.exists(name): if os.path.exists(name):
result.append(name) result.append(name)
else: else:
result = [] result = []
for dirname in list: for dirname in list:
sublist = glob1(dirname, basename) sublist = glob1(dirname, basename)
for name in sublist: for name in sublist:
result.append(path.join(dirname, name)) result.append(os.path.join(dirname, name))
return result return result
def glob1(dirname, pattern): def glob1(dirname, pattern):
if not dirname: dirname = '.' if not dirname: dirname = os.curdir
try: try:
names = posix.listdir(dirname) names = os.listdir(dirname)
except posix.error: except os.error:
return [] return []
names.sort()
result = [] result = []
for name in names: for name in names:
if name[0] <> '.' or pattern[0] == '.': if name[0] <> '.' or pattern[0] == '.':