else/Toolbox/fileswith.py
Ethan Dalool 6e64483523 else
2016-12-12 19:53:21 -08:00

35 lines
869 B
Python

'''
Search for a target string within the lines of files.
For example:
fileswith.py *.py "import random"
'''
import fnmatch
import glob
import re
import sys
from voussoirkit import spinal
filepattern = sys.argv[1]
searchpattern = sys.argv[2]
for filename in spinal.walk_generator():
filename = filename.absolute_path
if not fnmatch.fnmatch(filename, filepattern):
continue
matches = []
handle = open(filename, 'r', encoding='utf-8')
try:
with handle:
for (index, line) in enumerate(handle):
if re.search(searchpattern, line, flags=re.IGNORECASE):
line = '%d | %s' % (index, line.strip())
matches.append(line)
except:
pass
if matches:
print(filename)
print('\n'.join(matches).encode('ascii', 'replace').decode())
print()