[Tutor] Lists and removal
Roeland Rengelink
r.b.rigilink@chello.nl
Tue, 11 Sep 2001 08:48:48 +0200
Hi Jon,
At first sight, what you do looks OK.
One idea:
If ignore contains items that are not in dl. Then
for item in ignore:
dl.remove(item)
will raise a ValueError. Are you sure that you're not catching this
Exception
somewhere.
If that's not the case some judiously placed print stetements should
find the bug
For instance:
def getFile(text, dir):
hits = []
dl = os.listdir(dir)
print dl, ignore
for item in ignore:
print item, dl.index(item)
dl.remove(item)
print dl
text = string.lower(text)
for d in dl:
d = dir + '\\' + d
if os.path.isfile(d):
hits.extend(searchtext(d, text))
elif os.path.isdir(d):
hits.extend(getFile(text, d))
return hits
By the way: this function does two things. it searches for files, and
then it searches
for text in files. It might be a good idea to rewrite this as (untested)
def getFile(dir, ignore=[]):
'''recursevly search for files in dir, ignoring files from
ignore'''
hits = []
dl = os.listdir(dir)
for item in ignore:
try:
dl.remove(item)
except ValueError:
pass
for d in dl:
d = os.path.join(dir, d) # platform idependent
if os.path.isfile(d):
hits.extend([d])
elif os.path.isdir(d):
hits.extend(getFile(d))
return hits
use with:
result = []
for file in getFile(dir, ignore):
result.extend(searchtext(file, text))
Hope this helps,
Roeland
Jon Cosby wrote:
>
> Sorry, I got careless the first time. I was referring to the Python list
> operation, not removal from the mailing list. I imagine a lot of people
> ignored my first message. Let's try this again:
>
> I'm doing a Web search that is succesful until I try to configure it to
> ignore specified directories using the "remove" operation. I'm not getting
> any error messages, but the script stops when it comes to the function
> below. I've run the first few lines in the interpreter, and I can't see
> anything wrong with it. Any ideas?
>
> def getFile(text, dir):
> hits = []
> dl = os.listdir(dir)
> for item in ignore: # List of directories to ignore
> dl.remove(item) # Works in interpreter, but list comes up empty here
> text = string.lower(text)
> for d in dl:
> d = dir + '\\' + d
> if os.path.isfile(d):
> hits.extend(searchtext(d, text))
> elif os.path.isdir(d):
> hits.extend(getFile(text, d))
> return hits
>
> Jon Cosby
>
> jcosby@mindspring.com
> www.jcosby.com
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
--
r.b.rigilink@chello.nl
"Half of what I say is nonsense. Unfortunately I don't know which half"