前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 查找文件脚本 search_all.py

python 查找文件脚本 search_all.py

作者头像
用户5760343
发布2022-05-13 10:55:41
3490
发布2022-05-13 10:55:41
举报
文章被收录于专栏:sktj

""" ################################################################################ Use: "python ...\Tools\search_all.py dir string". Search all files at and below a named directory for a string; uses the os.walk interface, rather than doing a find.find to collect names first; similar to calling visitfile for each find.find result for "*" pattern; ################################################################################ """

import os, sys listonly = False textexts = ['.py', '.pyw', '.txt', '.c', '.h'] # ignore binary files

def searcher(startdir, searchkey): global fcount, vcount fcount = vcount = 0 for (thisDir, dirsHere, filesHere) in os.walk(startdir): for fname in filesHere: # do non-dir files here fpath = os.path.join(thisDir, fname) # fnames have no dirpath visitfile(fpath, searchkey)

def visitfile(fpath, searchkey): # for each non-dir file global fcount, vcount # search for string print(vcount+1, '=>', fpath) # skip protected files try: if not listonly: if os.path.splitext(fpath)[1] not in textexts: print('Skipping', fpath) elif searchkey in open(fpath).read(): input('%s has %s' % (fpath, searchkey)) fcount += 1 except: print('Failed:', fpath, sys.exc_info()[0]) vcount += 1

if name == 'main': searcher(sys.argv[1], sys.argv[2]) print('Found in %d files, visited %d' % (fcount, vcount))

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档