How to compare filenames in directory against a textfile list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • plotdevice
    New Member
    • May 2010
    • 3

    How to compare filenames in directory against a textfile list

    I have a text file that has a frequently-changing list of filenames in it.

    exampleList.txt :
    Code:
    fee.doc
    fye.doc 
    foe.doc
    fum.doc
    ftangftang_ole_biscuitbarrel.doc
    I want to return True if the list exactly matches the files in a given directory.
    I want to return False if the directory contains files that are not in the list, and also if it only contains some but not all of the files in the list.

    I know how to read the contents of the file into a variable, and I know how to read the directory listing into a variable. I know how to compare individual directory entries against the list, but no idea about comparing the entire directory listing at once against the textfile list. I've spent a stupidly huge amount of time on this already and I'm about ready to give up. Grr.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    You can test that with os.path.isfile( directory_and _file)
    Docs are here http://docs.python.org/library/os.path.html
    Also, you compare the length of the text file and the directory listing. If different, then they obviously are different. Finally then, loop through one of the lists (either text file records or directory listing) and delete from the other list if the file name is found in it. If you have any not found, or any left over then the two are different.

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      So am I right in saying you have two lists, and want to know if they are equal or one's a subset?

      You could then change both lists into sets, and use the set functions.

      If this doesn't mean anything, then post back and I'll explain in more detail.

      Comment

      Working...