python如何搜索关键字_Python遍历目录和搜索文件中的关键字
2012-11-03 wcdj
遗留问题:如何处理中文
#-*- coding: gbk -*-
############
# 20121003 wcdj
# FindFilev0.1
# 遍历目录和搜索文件中的关键字
############
# import module
import os
import sys
import cmd
# test
#print os.listdir('e:\\')
print "Beg"
print sys.argv
class Tool(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)# initialize the base class
self.pathname = "e://"
self.dirname = "test"
self.prompt = "(FindFile)>"
self.intro = '''FindFilev0.1 usage:
set # set dir and path parameters
export # export result file
find keyword # set finding keyword
? # help
exit # exit current program, or use Ctrl+D(UNIX)|Ctrl+Z(Dos/Windows)
'''
def help_exit(self):
print "quits the program"
def do_exit(self, line):
print "bye !"
sys.exit()
def help_set(self):
print "set parameter program used"
def do_set(self, choice):
print "default para is: "
print "pathname: '%s'" %self.pathname
print "dirname: '%s'" %self.dirname
choice = raw_input("Do you wanna change? y/n")
if choice == "y":
self.pathname = raw_input("input pathname: ")
self.dirname = raw_input("input dirname: ")
print "set pathname as: '%s'" %self.pathname
print "set dirname as: '%s'" %self.dirname
def help_find(self):
print "set finding keyword"
def do_find(self, keyword):
if keyword == "":
keyword = raw_input("input finding keyword: ")
print "set finding keyword as: '%s'" %keyword
GrepFromTxt(self.pathname + self.dirname, keyword)
print "find over !"
def help_export(self):
print "export result file"
def do_export(self, para):
print "pathname: '%s'" %self.pathname
print "dirname: '%s'" %self.dirname
WriteDirList(self.pathname + self.dirname, "output.txt")
print "export over !"
# write file lists in according path
def WriteDirList(path, file):
export = ""
for root, dirs, files in os.walk(path):
export += "\n%s %s %s" %(root, dirs, files)
open(file, "w").write(export)
# grep keyword from txt type file only
def GrepFromTxt(path, keyword):
filelist = os.listdir(path)
bFind = ""
print filelist
for file in filelist:
if ".txt" in file:
curfile = open(path + "//" + file)
print "finding %s..." %(curfile)
for line in curfile.readlines():
if keyword in line:
print line
bFind = "true"
if bFind != "true":
print "find nothing !"
##########
# start
##########
if __name__ == '__main__':
cdc = Tool()
cdc.cmdloop()
print "End" 输出格式: e://test ['2', '3'] ['code_test.c', '\xd0\xc2\xbd\xa8 \xce\xc4\xb1\xbe\xce\xc4\xb5\xb5.txt'] e://test\2 [] ['code_test.c'] e://test\3 [] ['aa.txt']
python如何搜索关键字_Python遍历目录和搜索文件中的关键字相关推荐
- python批量读取文件名_Python遍历目录并批量更换文件名和目录名的方法
本文实例讲述了Python遍历目录并批量更换文件名和目录名的方法.分享给大家供大家参考,具体如下: #encoding=utf-8 #author: walker #date: 2014-03-07 ...
- Python遍历目录下的文件(os.walk 、os.listdir的用法)
Python遍历目录下的文件(os.walk .os.listdir的用法) 最近想从一个目录转移一些文件到另外一个目录,因为文件有指定的后缀,所以想着用python来转移,不做cv工程师,那就涉及到 ...
- python学习笔记 os.scandir遍历目录
python学习笔记 os.scandir遍历目录 之前尝试用os.walk进行遍历目录,前几天看有人说os.scandir比walk更高效,尝试了一下,写了两端代码,做了一个测试 第一段用walk也 ...
- 搜索目录下 匹配文件中 最新的文件 路径
搜索目录下 匹配文件中 最新的文件 路径 1 #!/usr/bin/python 2 import os 3 import glob 4 path = "/homeyufei/tes ...
- Linux和Windows的遍历目录下所有文件的方法对比
首先两者读取所有文件的方法都是采用迭代的方式,首先用函数A的返回值判断目录下是否有文件,然后返回值合法则在循环中用函数B直到函数B的返回值不合法为止.最后用函数C释放资源. 1.打开目录 #inclu ...
- php 遍历 目录,PHP采用自定义函数实现遍历目录下所有文件的方法
目录的遍历是PHP程序设计中经常会用到的一个功能,很多PHP项目都有这一功能模块.今天本文就来实例解析一下PHP采用自定义函数实现遍历目录下所有文件的方法.具体方法如下: 方法一:使用readir() ...
- java文件tree目录_java 遍历目录,操作文件 tree命令
用tree命令显示目录.文件结构 有时候我们想了解一个文件夹或者驱动器根目录下的所有文件,并希望它以资源管理器的树形视图方式显示文件结构.可以在"命令提示符"窗口中输入" ...
- File类(常用方法、遍历目录下的文件、listFiles()方法和删除文件及目录)
File类 File类的常用方法 Filie类常用的构造方法 File(String pathname) //通过指定的一个字符串类型的文件路径来创建一个新的File对象 File(String pa ...
- Unity 遍历目录内所有文件并拷贝到指定目录
遍历目录内所有文件并拷贝到指定目录
最新文章
- Autofac3 在MVC4中的运用原理
- android 图片 编辑app,图片编辑工具手机版下载-图片编辑工具app下载8.33.107安卓官方版-西西软件下载...
- 201521123009 《Java程序设计》第12周学习总结
- Docker selenium自动化 - windows版docker的安装与运行环境检测
- 这款国产BI我推荐了:Tableau+SPSS+python的综合集成
- JS使用onscroll、scrollTop实现图片懒加载
- python+eclipse
- python中整数类型有—3_Python3 基本数据类型(3)
- C# using static 声明
- AD20更改PCB 2D视图的背景环境颜色
- PATH与CLASSPATH的区别
- Angularjs1 [$rootScope:inprog] inprogress error
- 安装BiocManager显示Warning in install.packages : package ‘BiocManager’ is not available for this version
- iOS-关于解决WebView、WKWEbView打开pdf文件乱码问题
- [JavaScript]45 Fresh Useful JavaScript and jQuery Techniques and Tools
- 路由器,交换机的存储介质的区别
- chai.js------使用
- 智能家电项目Linux程序,基于ARM-Linux的无线智能家居系统
- 新辰:舌尖上的中国2推起长沙90后糕富帅创业:买买提切糕店
- ubuntu16安装录屏软件
热门文章
- 驯龙大师稳了!iQOO Neo6游戏实测:两款手游均接近满帧
- 宁德时代考虑50亿美元在北美建厂 目标年产能最高80 GWh
- 扎克伯格夏威夷州大肆买地引发当地不满 被批搞“新殖民主义”
- 工信部:主要互联网企业开屏信息“关不掉”基本解决
- 自如:全额承担“望京跑路二房东”受害客户损失,预计约500余万元
- 库克警告员工不要泄露公司信息:将全力追查
- 已完成私有化交易 “网红第一股”如涵退市
- 又是low爆的借钱广告:被批三观扭曲,京东金融致歉
- 华为Mate40系列安兔兔跑分曝光:麒麟9000性能超骁龙865
- 五菱汽车:并不知悉导致股价及成交量上升的任何原因