-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFinder.py
More file actions
68 lines (55 loc) · 2.56 KB
/
SimpleFinder.py
File metadata and controls
68 lines (55 loc) · 2.56 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
from optparse import OptionParser
import optparse
import os
import re
def debug1(options, args):
print("sample use: " + '''python SimpleFinder.py -f klj -d "/c/OPENSW/" args1 args2''')
# http://stackoverflow.com/questions/6666856
# in python 2, all of your classes should inherit from object.
# If you don't, you end up with "old style classes", which are
# always of type classobj, and whose instances are always of type instance
print("type(options)\t\tis " + str(type(options))) # <type 'instance'>
print("options.__class__\tis " + str(options.__class__)) # optparse.values
print("type(optparse.Values)\tis " + str(type(optparse.Values))) # <type 'classobj'>
print("options:\t" + str(options))
print("vars(options):\t" + str(vars(options)))
print("args:\t\t" + str(args))
print("")
print("options.filename:\t" + str(options.filename ))
print("options.number +1:\t" + str(options.number + 1))
#print("options['filename']\t" + str(options['filename'])) #options is not a dic type
def parseOptions():
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
help="Find pattern in FILE", metavar="FILE")
parser.add_option("-d", "--dir", dest="directory",
help="Find pattern in all files in DIR", metavar="DIR")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
parser.add_option("-n", "--num",
action="store", dest="number", type="int", default=1,
help="number lucky")
# Program will return after next line if run with: <yourscript> [-h|--help]
(options, args) = parser.parse_args()
return (options, args)
def main():
#Parse input arguments
(options, args) = parseOptions();
#Print debug data
if options.verbose == True:
debug1(options, args)
pattern = re.compile(r'(.*doc)\s+.*')
#Attempt1 - walk through files in folder
#for filename in os.listdir (folder):
#Attempt2 - walk through files in folder
for root, dirs, filenames in os.walk(options.directory):
for f in filenames:
log = open(os.path.join(root, f),'r')
for lines in log:
output = pattern.findall(lines)
if output:
print(os.path.join(root, f) + " : " + output[0])
if __name__ == '__main__':
main()
#End of script