List comparison help please

Bucco phinsxiii at gmail.com
Sun Aug 20 17:47:14 EDT 2006


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




More information about the Python-list mailing list