-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathscript.py
More file actions
47 lines (40 loc) · 1.2 KB
/
script.py
File metadata and controls
47 lines (40 loc) · 1.2 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import re
import sys
from colorama import init, deinit, Fore, Back
REGEXPS = {
'-e': '[\w\d.]+@[\w\d.]+',
'-n': '-?[0-9]+',
}
BASE_DIR = 'data'
init()
try:
regexp = REGEXPS.get(sys.argv[1], sys.argv[1])
except IndexError:
print('''Usage:
script.py -e|-n|WORD
Find Emails or Numbers or WORD in data/
''')
sys.exit(1)
for filename in os.listdir(BASE_DIR):
fullpath = os.path.join(BASE_DIR, filename)
if not os.path.isfile(fullpath):
continue
found_smth = False
try:
for i, line in enumerate(open(os.path.join(BASE_DIR, filename)).readlines()):
match_obj = re.search(regexp, line)
if match_obj:
if not found_smth:
found_smth = True
print(Fore.GREEN + filename + Fore.RESET)
line = re.sub(regexp, lambda x: Fore.RED + x.group(0) + Fore.RESET, line)
print('{0}: {1}'.format(i, line), end='')
except UnicodeDecodeError:
# what are you going to do?
print(
Back.RED + Fore.WHITE +
'UnicodeDecodeError: file {0}, line {1}'.format(filename, i) +
Fore.RESET + Back.RESET)
pass
deinit()