1*cdf0e10cSrcweir#/usr/bin/env python 2*cdf0e10cSrcweir 3*cdf0e10cSrcweirimport re,os 4*cdf0e10cSrcweir 5*cdf0e10cSrcweirdef grep(pattern,dirname,names): 6*cdf0e10cSrcweir for name in names: 7*cdf0e10cSrcweir filename = os.path.join(dirname,name) 8*cdf0e10cSrcweir if os.path.isfile(filename): 9*cdf0e10cSrcweir lines = open(filename,"r").readlines() 10*cdf0e10cSrcweir for line in lines: 11*cdf0e10cSrcweir if pattern.search(line): 12*cdf0e10cSrcweir print filename 13*cdf0e10cSrcweir break 14*cdf0e10cSrcweir 15*cdf0e10cSrcweir 16*cdf0e10cSrcweirdef find(pattern,directory = "."): 17*cdf0e10cSrcweir os.path.walk(directory,grep,re.compile(pattern)) 18*cdf0e10cSrcweir 19*cdf0e10cSrcweir 20*cdf0e10cSrcweirif __name__ == "__main__": 21*cdf0e10cSrcweir import sys 22*cdf0e10cSrcweir if len(sys.argv) == 2: 23*cdf0e10cSrcweir find(sys.argv[1]) 24*cdf0e10cSrcweir elif len(sys.argv) == 2: 25*cdf0e10cSrcweir find(sys.argv[2],sys.argv[1]) 26