• 首页
  • Android
  • Java
  • Python
  • 信息安全
  • 闲扯淡

Guge's blog

以大多数人的努力程度之低,根本轮不到去拼天赋

遍历

python遍历文件,查找指定内容

2014年12月4日 by Guge Leave a Comment

比较笨,苦思了两天才写出这么十几行的代码,也顺便将Python的正则和os.path.walk学习了一下。

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import os, sys, re
 
def searcher(root_dir):
    skipexts = ['.gif', '.exe', '.pyc', '.o', '.a','.dll','.lib','.pdb','.mdb']
    regex = re.compile(r'http:\/\/.*')
    for root,dirs,files in os.walk(root_dir):
        for file in files:
            if os.path.splitext(file)[1] not in skipexts:
                file_obj = open(os.path.join(root, file),'rU')
                line_no = 0
                for eachline in file_obj:
                    line_no += 1
                    if regex.findall(eachline):
                         print "%s %d:%s"%(os.path.join(root, file),line_no,eachline)
searcher('/home/')

效果是从指定的目录下,遍历所有文件内容,并从文件内容中查找指定的关键字,并告知在具体哪个文件的哪一行,我这里使用了正则从文本内容中匹配URL地址

Posted in: Python Tagged: 查找, 正则, 遍历

遍历指定目录下所有文件并查找关键字(转)

2014年12月3日 by Guge Leave a Comment

最近刚好在学习这方面的知识,在OsChina上看到了这段代码,特转来留存:

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os, sys
listonly = False
skipexts = ['.gif', '.exe', '.pyc', '.o', '.a','.dll','.lib','.pdb','.mdb']        # ignore binary files
      
def 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 += 1
     
def 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)

 

Posted in: Python Tagged: 查找, 遍历

微信公众平台

站内搜索

标签

360 Activity ADB Android android studio apktool arm BCTF CSRF CTF drozer hacker精神 IDA ISG java线程 Json Launch4j MySQL ndk Phantomjs python ROP xposed xss Zaker 一周安全 信息安全 信息安全,干货 加壳 华尔街之狼 安全 安全干货 安全竞赛 寄存器 干货 数据库 查找 步长 每天一个小程序 爬虫 程序员 系统信息获取 脱壳 逆向 遍历

近期文章

  • 关于绕过域名(ip)的一些小技巧
  • 骨哥电台第4期:马斯克之地启示录1
  • 骨哥电台第3期:了解马斯克
  • 骨哥电台第2期:钢铁侠原型-霍华德·休斯的故事
  • 它终于让我换下了使用多年的谷歌浏览器

友情链接

CRoot' Blog
void* Future

Copyright © 2021 Guge's blog.

Omega WordPress Theme by ThemeHall