- 论坛徽章:
- 1
|
回复 4# __daydayup__ - #!/usr/bin/env python
- #coding:utf-8
- import os,glob
- def search_files(pattern,search_path):
- if not search_path:
- return []
- childpathes = []
- matches = []
- for spath in search_path:
- matches += [os.path.join(spath,f) for f in glob.glob1(spath, pattern) if os.path.isfile(os.path.join(spath, f)) ]
- for p in os.listdir(spath):
- childpath = os.path.join(spath, p)
- if os.path.isdir(childpath):
- childpathes.append(childpath)
- return matches + search_files(pattern, childpathes)
- print search_files("*.tpl", ["/tmp/tmp"])
复制代码 结果- $ python search.py
- ['/tmp/tmp/a.tpl', '/tmp/tmp/tmp/b.tpl', '/tmp/tmp/tmp/tmp/c.tpl', '/tmp/tmp/tmp/tmp/tmp/d.tpl']
复制代码 |
|