- 帖子
- 27
- 精华
- 0
- 积分
- 389
- 阅读权限
- 30
- 注册时间
- 2013-9-9
- 最后登录
- 2014-5-20
|
回帖奖励 +5
本帖最后由 fl0w 于 2013-12-26 22:35 编辑
findThemAll.py- #! /usr/bin/env python
- # coding:utf-8
- import sys
- import os
- import fnmatch
- import re
- def filterFilename(path, text):
- fileList = []
- for dirpath, dirnames, filenames in os.walk(path):
- for filename in filenames:
- if fnmatch.fnmatch(filename, text):
- fileList.append(os.path.join(dirpath, filename))
- return fileList
- if __name__ == "__main__":
- if len(sys.argv) < 3:
- print 'Please input like this:./findThemAll.py your_dir your_match_string'
- elif os.path.exists(sys.argv[1]):
- fileList = filterFilename(sys.argv[1],'*.txt')
- themAll = []
- for file in fileList:
- with open(file) as f:
- content = f.read()
- if len(re.findall(sys.argv[2], content)) > 0:
- themAll.append(file)
- print 'The files under %s contain "%s" are:' % (sys.argv[1], sys.argv[2])
- print themAll
- else:
- print 'Are you sure the path is exist?'
复制代码 |
|