Bucco wrote: > 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 -- http://mail.python.org/mailman/listinfo/python-list