|
1 # Copyright (c) 2008 Nokia Corporation |
|
2 # |
|
3 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 # you may not use this file except in compliance with the License. |
|
5 # You may obtain a copy of the License at |
|
6 # |
|
7 # http://www.apache.org/licenses/LICENSE-2.0 |
|
8 # |
|
9 # Unless required by applicable law or agreed to in writing, software |
|
10 # distributed under the License is distributed on an "AS IS" BASIS, |
|
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 # See the License for the specific language governing permissions and |
|
13 # limitations under the License. |
|
14 |
|
15 # Lists all the files, with the absolute path, matching the specified pattern. |
|
16 import os, fnmatch |
|
17 |
|
18 # From Python Cookbook, 2nd ed., by Alex Martelli, Anna Martelli |
|
19 # Ravenscroft, and David Ascher (O'Reilly Media, 2005) 0-596-00797-3 |
|
20 def all_files(root, patterns='*', single_level=False, yield_folders=False): |
|
21 # Expand patterns from semicolon-separated string to list |
|
22 patterns = patterns.split(';') |
|
23 for path, subdirs, files in os.walk(root): |
|
24 if yield_folders: |
|
25 files.extend(subdirs) |
|
26 files.sort() |
|
27 for name in files: |
|
28 for pattern in patterns: |
|
29 if fnmatch.fnmatch(name, pattern): |
|
30 yield os.path.join(path, name) |
|
31 break |
|
32 if single_level: |
|
33 break |