标签:pen ble .cpp std sts ror odi open com
经常有这样的需求:在一个文本文件里查找特定字符串,这很好实现,用任何文本查看工具几乎都可以做到。而有的时候,想查找一个文件夹下的所有文本文件(特定后缀),我就遇到了这样的问题:想找到Blender的源代码中关于SPH的实现代码。于是写了下面的简单程序:
#!/usr/bin/env python3 import os def Search(rootDir, suffixes, arg): for lists in os.listdir(rootDir): path = os.path.join(rootDir, lists) if os.path.isfile(path): if path.endswith(suffixes): try: with open(path, encoding=‘utf_8‘) as fh: lineNum = 0 for line in fh: lineNum += 1 if arg in line: print(lineNum, ‘:‘, path, ‘\n‘, line) fh.close() except: print(‘error: ‘, path, ‘\n‘) if os.path.isdir(path): Search(path, suffixes, arg) Search(r‘D:\blender-2.70‘, (‘.c‘,‘.cpp‘,‘.h‘,‘.hpp‘), ‘SPH ‘)
程序虽小,但很实用,运行结果如下:
标签:pen ble .cpp std sts ror odi open com
原文地址:http://www.cnblogs.com/zhaolizhe/p/6937983.html