-
Notifications
You must be signed in to change notification settings - Fork 0
/
Seeker1.txt
27 lines (25 loc) · 1.37 KB
/
Seeker1.txt
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
import os
from os.path import join
keyword = input("Search For?: ") # ask the user for keyword, use raw_input() on Python 2.x
root_dir = 'C:\\' # path to the root directory to search
for root, dirs, files in os.walk(root_dir, onerror=None): # walk the root dir
directory = os.listdir (root)
for filename in files: # iterate over the files in the current dir
file_path = os.path.join(root, filename) # build the file path
try:
with open(file_path, "rb") as f: # open the file for reading
# read the file line by line
for line in f: # use: for i, line in enumerate(f) if you need line numbers
try:
line = line.decode("utf-8") # try to decode the contents to utf-8
except ValueError: # decoding failed, skip the line
continue
if keyword in line: # if the keyword exists on the current line...
print(" ")
print(line) #print the line.
print(" ")
print(file_path) # print the file path
print(directory)
# no need to iterate over the rest of the file
except (IOError, OSError): # ignore read and permission errors
pass