List comparison help please

B

Bucco

I am trying to compare a list of items to the list of files generated
by os.listdir. I am having trouble getting this to work and think I
may be going down the wrong path. Please let me know if hter is a
better way to do this. THis is what I have for my class so far:

import os, sys

class MatchList:
def __init__(self, dir, file):
self.dir = os.listdir(dir)
self.flist = open(file, 'r').readlines()
self.matches = []
def matcher(self):
#~ matches = []
for fname in self.dir:
#~ print fname
if fname[-4] == '.':
for item in self.flist:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)
#~ else:
#~ break
#~ if self.matches == '':
#~ print "Sorry, there was no match."
#~ else:
#~ for item in matches:
#~ print item
def matchedFiles(self):
for item in self.matches: print item

if __name__ == '__main__':
dir = sys.argv[1]
file = sys.argv[2]
list = open(file, 'r').readlines()
test = MatchList(dir, file)
test.matcher()
test.matchedFiles()


Thanks in advance for your help.

:)
SA
 
F

Felipe Almeida Lessa

20 Aug 2006 14:47:14 -0700 said:
I am trying to compare a list of items to the list of files generated
by os.listdir. I am having trouble getting this to work and think I
may be going down the wrong path. Please let me know if hter is a
better way to do this. THis is what I have for my class so far:

Have you tried using sets?
['lost+found', 'var', 'etc', 'media', 'cdrom', 'bin', 'boot', 'dev',
'home', 'initrd', 'lib', 'mnt', 'opt', 'proc', 'root', 'sbin', 'srv',
'sys', 'tmp', 'usr', 'initrd.img', 'vmlinuz', 'windows',
'initrd.img.old', 'vmlinuz.old']
s = set(os.listdir('/'))
p = set(['opt', 'mnt', 'initrd', 'home', 'tmp', 'lib', 'media',
'boot', 'usr', 'var', 'proc', 'bin', 'sys', 'initrd.img.old', 'cdrom',
'lost+found', 'sbin', 'vmlinuz.old', 'windows'])set(['dev', 'etc', 'vmlinuz', 'srv', 'root', 'initrd.img'])
 
S

Simon Forman

Bucco said:
I am trying to compare a list of items to the list of files generated
by os.listdir. I am having trouble getting this to work and think I
may be going down the wrong path. Please let me know if hter is a
better way to do this. THis is what I have for my class so far:

import os, sys

class MatchList:
def __init__(self, dir, file):
self.dir = os.listdir(dir)
self.flist = open(file, 'r').readlines()
self.matches = []
def matcher(self):
#~ matches = []
for fname in self.dir:
#~ print fname
if fname[-4] == '.':
for item in self.flist:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)
#~ else:
#~ break
#~ if self.matches == '':
#~ print "Sorry, there was no match."
#~ else:
#~ for item in matches:
#~ print item
def matchedFiles(self):
for item in self.matches: print item

if __name__ == '__main__':
dir = sys.argv[1]
file = sys.argv[2]
list = open(file, 'r').readlines()
test = MatchList(dir, file)
test.matcher()
test.matchedFiles()


Thanks in advance for your help.

:)
SA

0) What kind of trouble?

1) Don't use "dir", "file", and "list" as variable names, those are
already python built in objects (the dir() function, list type, and
file type, respectively.)

2) 'r' is the default for open(), omit it. "self.flist =
open(file).readlines()"

3) The output of file.readlines() includes the newline at the end of
each line. Use the rstrip() method of strings if this is a problem for
you:

lines = [line.rstrip() for line in open(file).readlines()]

4) This is bizarre:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)

why not "if item != fname[:-4]: self.matches.append(fname)"? But,
note, you're appending fname to self.matches once for every item in
self.flist that it does not match. You might want to put a break
statement in there:

if item != fname[:-4]:
self.matches.append(fname)
break

But see the next point:

5) Since you have a list of things you're matching (excluding actually)
this part:
for item in self.flist:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)

could become:

if fname[:-4] not in self.flist: self.matches.append(fname)

6) Why are you using #~ for comments?

Also, check out os.path.splitext()
http://docs.python.org/lib/module-os.path.html#l2h-1761

HTH,
~Simon
 
B

Bucco

Simon said:
1) Don't use "dir", "file", and "list" as variable names, those are
already python built in objects (the dir() function, list type, and
file type, respectively.)

Thanks. My own stupidity on this one.

2) 'r' is the default for open(), omit it. "self.flist =
open(file).readlines()"

Could you clarify? Show an example. Sorry if I sound newbyish, but I
am.

5) Since you have a list of things you're matching (excluding actually)
this part:
for item in self.flist:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)

could become:

if fname[:-4] not in self.flist: self.matches.append(fname)

Thanks. This was what I was looking for.

6) Why are you using #~ for comments?

This is just the automatic commenting on scite. I put my cursor on the
line I want to comment and press ctrl-q ans scite comments out the
whole line or selection. As for why the lines are commented; I
commented these lines so I could test out some of the code prior to
running the next bit.
I will check this out also.

Thank You for your help. You have answered some questions for me.

Thanks:)

SA
 
S

Simon Forman

Bucco said:
Thanks. My own stupidity on this one.



Could you clarify? Show an example. Sorry if I sound newbyish, but I
am.

Sure, no problem. "open(filename, 'r')" is exactly the same as
"open(filename)". The flag argument is optional and when you leave it
off it defaults to 'r'.
5) Since you have a list of things you're matching (excluding actually)
this part:
for item in self.flist:
if item == fname[:-4]:
pass
else:
self.matches.append(fname)

could become:

if fname[:-4] not in self.flist: self.matches.append(fname)

Thanks. This was what I was looking for.

Cool! You're welcome. :)
This is just the automatic commenting on scite. I put my cursor on the
line I want to comment and press ctrl-q ans scite comments out the
whole line or selection. As for why the lines are commented; I
commented these lines so I could test out some of the code prior to
running the next bit.

Ah, right on. I was just wondering.
I will check this out also.

Thank You for your help. You have answered some questions for me.

Thanks:)

SA

You're very welcome. It's a pleasure.

Peace,
~Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top