如何用Python os.path.walk方法遍历搜索文件内容的操作详解,pythonos.path.walk,本文是关于如何用Pyth
如何用Python os.path.walk方法遍历搜索文件内容的操作详解,pythonos.path.walk,本文是关于如何用Pyth
本文是关于如何用Python os.path.walk方法遍历搜索文件目录内容的操作详解的文章,python 代码中用os.path.walk函数这个python模块的方法来遍历文件,python列出文件夹下的所有文件并找到自己想要的内容。
文中使用到了Python os模块和Python sys模块,这两个模块具体的使用方法请参考编橙之家相关文章阅读。
Python os.path.walk方法遍历文件搜索内容方法代码如下:
import os, sys#代码中需要用到的方法模块导入listonly = Falseskipexts = ['.gif', '.exe', '.pyc', '.o', '.a','.dll','.lib','.pdb','.mdb'] # ignore binary files def visitfile(fname, searchKey): 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 += 1 #www.iplaypy.comdef visitor(args, directoryName,filesInDirectory): 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)
如何用Python os.path.walk方法遍历搜索文件内容的操作详解,文章所用到的模块方法相关文章推荐:
Python OS模块;
Python sys模块;
Python import语句导入模块语法;
编橙之家文章,
相关内容
- Python脚本随机生成中文验证码源码实例分析,python实例
- Python标准库模块之Sys使用详解,python使用详解,本文主要
- 本地服务更新Python代码 如要使用请适当的修改,pytho
- python抽奖 系统算法代码的简单实现,python抽奖算法代码
- 如何用Python代码实现自动比较两个文件中的代码变化?
- python logging 日志模块使用方法学习,pythonlogging,本文为
- Python近期使用较少算法实现方法,python近期较少算法
- 用Python来处理中文分句的方法_【源码精华】,,我在用
- 用Python生成随机的中文验证码图片,,在登录很多网站的
- Python方法批量重命名MP3文件,python重命名,Python方法批量
评论关闭