遍历文件和文件夹oswalk

懂oswalk的就不要笑我了,我只是随笔记下之前的工作

执行效果类似于linux tree 命令, 遍历所有的文件,按照需求进行处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import os, fnmatch

def all_files(root, patterns = '*', single_level = False, yield_folders=False):
patterns = patterns.split(';')
for path, subdirs, files in os.walk(root):
if yield_folders:
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
if fnmatch.fnmatch(name, pattern):
yield os.path.join(path,name)
break
if single_level:
break

if __name__ == '__main__':
for path in all_files('/home/dan/ownCloud/Multimedia/GPU/new/GPU','*.py;*html;*pdf'):
print path