-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindFileContaining.py
More file actions
executable file
·90 lines (77 loc) · 2.68 KB
/
findFileContaining.py
File metadata and controls
executable file
·90 lines (77 loc) · 2.68 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# @author: technocake
# @desc : finds all lines in files in a dir that contains what you're searching for.
#
# version: 0.1
#
# 06.05.2011 technocake made version 0.1 - no recursive
# 09.05.2011 technocake Added recursive support to it
# 28.05.2011 technocake Added the line output + match group to the output :)
# I.e, you see the content of the line too.
# 29.05.2011 technocake Added some "narrow" binary support. I.e for searching pdfs.
# Not quite adequite
#
#######################################################################################
import sys, re, os
def usage():
return """
Usage: %s '<search for>' '<in dir> [-R]'
finds all files containing <search for> in the given dir
The -R flag makes the search go recursively
""" % (sys.argv[0])
class Config:
""" The config class """
conf = Config()
def parse():
""" pares() : Parses cli params. Checks for recursive mode and configures the module"""
if not len(sys.argv) >= 3:
print ( usage() )
sys.exit()
#setting configs
(conf.searchString, conf.rootDir) = sys.argv[1:3]
args = sys.argv[2:]
#The reason why I love python :)
conf.recursive = "-R" in args
def find_file_containing(string, directory, pad=""):
directory = os.listdir(directory)
results = {}
folders = []
binaryFileTypes = ["pdf", "doc","docx"]
output = ""
for f in directory:
if os.path.isdir(f):
folders.append(f)
if not os.path.isfile(f): continue
line_number = 0
#if not re.search("\.pdf$|\.PDF$", f) == None:i
#Reads binary if binary filetype
if f.split(".")[-1].lower() in binaryFileTypes:
readType = "rb"
#Reads normaly (text) if not binary file.
else:
readType = "rU"
fileData = open(f, readType).readlines()
for line in fileData:
line_number += 1
#Searching line for line with regexp
matches = re.search( string, line)
if not matches == None:
output += "%sFound match in file %s at line: %d\n" % (pad, f, line_number )
output += "%s Group: %s \t Data: \n\t%s%s\n" % (pad, matches.group(), pad, line )
if conf.recursive:
for d in folders:
output += "\n\n%s searching %s\n " %(pad, d,)
output += "%s\t%s\n" %(pad, find_file_containing(string, "%s/%s"%(conf.rootDir, d), "%s\t"%(pad,)))
return output
#If this is not imported as a module, it should do this:
#By this, I mean run as a script.
if __name__ == "__main__":
parse()
print ( "Searching for files containing: %s in dir: %s"
% (conf.searchString, conf.rootDir) )
print ( "Recursive mode : ", conf.recursive )
#doing the search
try:
print( find_file_containing(conf.searchString, conf.rootDir) )
except KeyboardInterrupt: #a.k.a Ctrl + C
print ( "\n Stopping search - interrupted by user ;) " )