Python os.path.walk遍历文件,搜索文件里面的内容
来源:互联网 发布:易语言免费网络验证 编辑:程序博客网 时间:2024/05/29 13:21
用os.path.walk来遍历文件,并且搜索文件里面的内容。
import os, syslistonly = Falseskipexts = ['.gif', '.exe', '.pyc', '.o', '.a','.dll','.lib','.pdb','.mdb'] # ignore binary filesdef visitfile(fname, searchKey): # for each non-dir file global fcount, vcount try: if not listonly: if os.path.splitext(fname)[1] in skipexts: pass elif open(fname).read().find(searchKey) != -1: print'%s has %s' % (fname, searchKey) fcount += 1 except: pass vcount += 1def visitor(args, directoryName,filesInDirectory): # called for each dir for fname in filesInDirectory: fpath = os.path.join(directoryName, fname) if not os.path.isdir(fpath): visitfile(fpath,args)def searcher(startdir, searchkey): global fcount, vcount fcount = vcount = 0 os.path.walk(startdir, visitor, searchkey)if __name__ == '__main__': root=raw_input("type root directory:") key=raw_input("type key:") searcher(root,key) print 'Found in %d files, visited %d' % (fcount, vcount)
运行结果如下所示:
sina@ubuntu:~/work/python$ python search_content.pytype root directory:/home/sina/work/pythontype key:sound/home/sina/work/python/pygame.CHM has sound/home/sina/work/python/game.py has soundFound in 2 files, visited 90
- Python os.path.walk遍历文件,搜索文件里面的内容
- Python os.path.walk遍历文件,搜索文件里面的内容
- 用os.path.walk来遍历文件,并且搜索文件里面的内容
- Python os.walk文件遍历
- python使用os.walk和os.path.join来遍历文件夹的文件(包括子文件夹下的文件)
- python使用os.walk和os.path.join来遍历文件夹的文件(包括子文件夹下的文件)
- python使用os.walk和os.path.join来遍历文件夹的文件(包括子文件夹下的文件)
- Python递归查找文件(os.walk, os.path, fnmatch)
- Python文件遍历os.walk()与os.listdir()
- python学习笔记之os.walk()实现遍历文件搜索字符串
- 文件遍历os.walk方法
- os.walk( )遍历指定目录下的所有文件
- [python] os.walk()与os.path.walk()
- python os.walk和os.path.walk
- Python os.walk() 和 os.path.walk()
- python os.walk和os.path.walk
- Python os.walk()和os.path.walk()
- 使用python的os.walk()包来监测文件变化
- js 判断是否为 Android IOS IPAD IPHONE 等移动设备访问
- sdio架构初解
- java中main函数解析
- 关于禁止webbrowser在触屏电脑上的缩放,找了很多方法,现在总结一下
- linux test
- Python os.path.walk遍历文件,搜索文件里面的内容
- linux设备驱动——andriod平台wlan驱动
- 什么是装箱和拆箱?怎样操作?
- ucGUI文件说明
- 刚毕业没几年,别去计较太多得失
- Cannot call method 'indexOf' of undefined
- 利用正则表达式判断身份证号
- 微信公众平台消息接口-java-jsp版
- 基于ARM含SD控制器的SD卡的SDIO模式驱动解析