| File Searcher |
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
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
from UserList import UserList
from os import listdir, curdir
from os.path import isfile, isdir
from os.path import join as joinpath
from os.path import split as splitpath
# File Searcher coded by digitalchameleon
class filespider:
def __init__(self,action,verbose=False):
self.verbose=verbose
self.action=action
def __scanpath(self,path):
if self.verbose:print "scanning:"+path
try:
items=listdir(path)
for item in items:
itempath=joinpath(path,item)
self.action(itempath)
if isdir(itempath):
if not itempath in self.pastpaths:
self.futurepaths.append(itempath)
except WindowsError:
print 'WindowsError encountered at:'+path
self.pastpaths.append(path)
def start(self,root):
self.pastpaths=[]
self.futurepaths=[]
if not isdir(root):
print "that's not a directory!"
return -1
print "scanning"
print root
self.futurepaths.append(root)
while self.futurepaths:
self.__scanpath(self.futurepaths.pop())
print 'scan complete'
class filterlist(UserList):# a list which is filtered by a given function
def __init__(self, filterfunction,initlist=None):
self.data = []
initlist=None
self.function=filterfunction
def append(self, item):
if self.function(item):
self.data.append(item)
def insert(self, i, item):
if self.function(item):
self.data.insert(i, item)
def isfoundin(keyword, string, capsense=False):
return keyword.lower() in string.lower()
rootpath=raw_input("Root path to begin searching at:")
keyword=raw_input("Keyword to search for:")
MyHits=filterlist(lambda x:isfoundin(keyword,splitpath(x)[-1]))
# note the use of lambda here. filterlist will pass one arguement,
# but isfoundin requires two. there must be a better way, but this works.
FileCrawler=filespider(MyHits.append)
FileCrawler.start(rootpath)
for i in MyHits:
print i
Parsed in 0.031 seconds, using GeSHi 1.0.8.6
Please Login to Post a Comment.
Rating is available to Members only.
Please login or register to vote.
Please login or register to vote.
No Ratings have been Posted.


