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







原创粉丝点击