文章来自《Python cookbook》. 翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!
@.84@ [DateTime(2004-09-27T09:11:33Z)] TableOfContents
描述
...Walking Directory Trees Credit: Robin Parmar, Alex Martelli
问题 Problem
You need to examine a directory, or an entire directory tree rooted in a certain directory, and obtain a list of all the files (and optionally folders) that match a certain pattern.
解决 Solution
os.path.walk is sufficient for this purpose, but we can pretty it up quite at bit:
import os.path, fnmatch
def listFiles(root, patterns='*', recurse=1, return_folders=0):
- # Expand patterns from semicolon-separated string to list pattern_list = patterns.split(';') # Collect input and output arguments into one bunch class Bunch:
- def _ _init_ _(self, **kwds): self._ _dict_ _.update(kwds)
- return_folders=return_folders, results=[])
- # Append to arg.results all relevant files (and perhaps folders) for name in files:
- fullname = os.path.normpath(os.path.join(dirname, name)) if arg.return_folders or os.path.isfile(fullname):
- for pattern in arg.pattern_list:
- if fnmatch.fnmatch(name, pattern):
- arg.results.append(fullname) break
- if fnmatch.fnmatch(name, pattern):
- for pattern in arg.pattern_list:
- fullname = os.path.normpath(os.path.join(dirname, name)) if arg.return_folders or os.path.isfile(fullname):
讨论 Discussion
The standard directory-tree function os.path.walk is powerful and flexible, but it can be confusing to beginners. This recipe dresses it up in a listFiles function that lets you choose the root folder, whether to recurse down through subfolders, the file patterns to match, and whether to include folder names in the result list.
The file patterns are case-insensitive but otherwise Unix-style, as supplied by the standard fnmatch module, which this recipe uses. To specify multiple patterns, join them with a semicolon. Note that this means that semicolons themselves can't be part of a pattern.
For example, you can easily get a list of all Python and HTML files in directory /tmp or any subdirectory thereof:
thefiles = listFiles('/tmp', '*.py;*.htm;*.html')
参考 See Also
Documentation for the os.path module in the Library Reference.