Python脚本实现查找指定目录下包含指定关键字的文件
2016-07-04 15:07
1256 查看
#!/usr/bin/python # coding:utf8 import os # 判断文件中是否包含关键字,是则将文件路径打印出来 def is_file_contain_word(file_list, query_word): for _file in file_list: if query_word in open(_file).read(): print _file print("Finish searching.") # 返回指定目录的所有文件(包含子目录的文件) def get_all_file(floder_path): file_list = [] if floder_path is None: raise Exception("floder_path is None") for dirpath, dirnames, filenames in os.walk(floder_path): for name in filenames: file_list.append(dirpath + '\\' + name) return file_list query_word = raw_input("Please input the key word that you want to search:") basedir = raw_input("Please input the directory:") is_file_contain_word(get_all_file(basedir), query_word) raw_input("Press Enter to quit.")
相关文章推荐
- Python 语言及其应用 Chapter_5_Note_4 Counter()计数器
- python中几种不同类型的参数的区分及其定义时的顺序
- Python 语言及其应用 Chapter_5_Note_3 defaultdict_setdefault
- String to Integer (atoi)(python)
- python的strip()函数使用
- Python菜鸟之路:Python基础-Socket基础-1
- Python Socket 编程,OSError: [Errno 98] Address already in use
- python 的公有和“私有”
- windos7中配置python的环境变量
- 使用sched库完成周期定时任务
- Python读取文件小结(csv,txt)
- 使用Python学习selenium测试工具-6:同步
- python INFO: Can't locate Tcl/Tk libs and/or headers
- 使用Python学习selenium测试工具-5:元素交互
- python深浅拷贝探究
- Json概述以及python对json的相关操作
- python中字符串的使用方法
- Python函数式编程指南(三):迭代器
- mac下python安装lxml失败
- 基于树的建模-完整教程(R & Python)