[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
>>>
>>> list(os.popen(r'c:\cygwin\bin\ls parts/part*'))
['parts/part0001\n', 'parts/part0002\n', 'parts/part0003\n', 'parts/part0004\n']

The next two alternative techniques do better on both counts.


The glob module


The term globbing comes from the wildcard character in filename patterns; per com-
puting folklore, a
matches a “glob” of characters. In less poetic terms, globbing simply
means collecting the names of all entries in a directory—files and subdirectories—
whose names match a given filename pattern. In Unix shells, globbing expands filename
patterns within a command line into all matching filenames before the command is
ever run. In Python, we can do something similar by calling the glob.glob built-in—a
tool that accepts a filename pattern to expand, and returns a list (not a generator) of
matching file names:


>>> import glob
>>> glob.glob('*')
['parts', 'PP3E', 'random.bin', 'spam.txt', 'temp.bin', 'temp.txt']

>>> glob.glob('*.bin')
['random.bin', 'temp.bin']

>>> glob.glob('parts')
['parts']

>>> glob.glob('parts/*')
['parts\\part0001', 'parts\\part0002', 'parts\\part0003', 'parts\\part0004']

>>> glob.glob('parts\part*')
['parts\\part0001', 'parts\\part0002', 'parts\\part0003', 'parts\\part0004']

The glob call accepts the usual filename pattern syntax used in shells:? means any one
character, * means any number of characters, and [] is a character selection set.‡ The
pattern should include a directory path if you wish to glob in something other than the
current working directory, and the module accepts either Unix or DOS-style directory
separators (/ or ). This call is implemented without spawning a shell command (it uses
os.listdir, described in the next section) and so is likely to be faster and more portable
and uniform across all Python platforms than the os.popen schemes shown earlier.


Technically speaking, glob is a bit more powerful than described so far. In fact, using
it to list files in one directory is just one use of its pattern-matching skills. For instance,
it can also be used to collect matching names across multiple directories, simply because
each level in a passed-in directory path can be a pattern too:


>>> for path in glob.glob(r'PP3E\Examples\PP3E\*\s*.py'): print(path)
...

‡ In fact, glob just uses the standard fnmatch module to match name patterns; see the fnmatch description in
Chapter 6’s find module example for more details.


166 | Chapter 4: File and Directory Tools

Free download pdf