-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-text-in-file.py
46 lines (29 loc) · 1.04 KB
/
find-text-in-file.py
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
import os
import mimetypes
def is_binay_file(filepathname):
textchars = bytearray([7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x7f)) + bytearray(range(0x80, 0x100))
is_binary_string = lambda bytes: bool(bytes.translate(None, textchars))
if is_binary_string(open(filepathname, 'rb').read(1024)):
return True
else:
return False
to_find = "_checkKey"
counter = 0
for root, dirs, files in os.walk("/var/www/html/bookmarker"):
for f in files:
file_name = root + '/' + f
with open(file_name, 'r') as file :
if not is_binay_file(file_name):
try:
content = file.read()
if content.find(to_find) != -1:
counter += 1
print (file_name + '\n\n')
except UnicodeDecodeError:
continue
if counter > 1:
print (str(counter) + " files found")
elif counter == 1:
print (str(counter) + " file found")
else:
print ("No file found")