python脚本:搜索某一目录下出现特定字符串的文件
2009-12-16 22:31
1316 查看
2009年10月31日 星期六 23:55 from os.path import walk, join, normpath from os import chdir, remove import re import os import sys import getopt def find_file_by_pattern(pattern='.*', base=".", circle=True): re_file = re.compile(pattern) if base == ".": base = os.getcwd() final_file_list = [] print "You are searching " + pattern + " under folder: " + base + "..." cur_list = os.listdir(base) for item in cur_list: full_path = os.path.join(base, item) if full_path.endswith(".doc") or full_path.endswith(".bmp") or\ full_path.endswith(".wpt") or full_path.endswith(".dot"): continue #print full_path bfile = os.path.isfile(item) #print bfile if os.path.isfile(full_path): print "Searching[" + pattern + "]..." fp = open(full_path) searchitem = fp.read() #if re_file.search(full_path): if re_file.search(searchitem): final_file_list.append(full_path) print "********* Find: " + full_path else: #非文件则递归查找其下的文件 final_file_list += find_file_by_pattern(pattern, full_path) return final_file_list #if __name__ == '__main__': opts, args = getopt.getopt(sys.argv[1:], "f:p:", ["file=", "pattern="]) for o, f in opts: global myPattern global myFile if o in ("-f", "--file") : myFile=f #print "file" + myFile if o in ("-p", "--pattern") : myPattern=f #print "pattern" + myPattern result = find_file_by_pattern(myPattern, myFile) for item in result: print "Find: " + item
相关文章推荐
- python脚本-指定目录针对特定后缀名文件进行文本内容搜索
- Linux下搜索指定目录下文件中特定字符串并高亮显示匹配关键词
- 涛哥的Python脚本工具箱之批量替换目录所有指定扩展名的文件中的指定字符串
- Python---对html文件内容进行搜索取出特定URL地址字符串,保存成列表,并使用每个url下载图片,并保存到硬盘上,使用正则re
- python---字符串的拼接、去重、反转、字母花样排序、单词出现判断、统计文件特定单词频率lambda、硬盘容量、列表转字符串
- Python---对html文件内容进行搜索取出特定URL地址字符串,保存成列表,并使用每个url下载图片,并保存到硬盘上,使用bs4,beautifulsoup模块
- 子目录下,grep搜索子目录中包含某字符串的特定文件
- python实操(1):搜索指定目录(文件)下包含指定字符串的文件,及行号
- Python 脚本 批量删除目录下文件的特定内容
- 自定义VBS脚本(统计在指定文件中搜索字符串出现的次数)
- ubuntu中在终端下如何查找具有某一特定字符串的文件
- Python脚本删除VC临时文件及Debug目录
- 用python查找在指定目录下特定文件夹下的指定文件
- dos下搜索当前目录及其子目录下的所有特定后缀名的文件并拷贝到新文件夹
- Shell脚本执行hive语句 | hive以日期建立分区表 | linux schedule程序 | sed替换文件字符串 | shell判断hdfs文件目录是否存在
- python读写文件,分析字符串及读取目录下文件名的实践
- Python删除指定目录下过期文件的2个脚本分享
- python某一目录下指定格式的文件路径保存到指定格式的文件中
- 查找当前目录下包含特定字符串的特殊后缀名的文件
- 基于Python SimpleHTTPServer.py的修改脚本:HTTP文件服务器,修正中文目录列表,支持视频文件在线播放