[Tutor] Location of found item in list.

Luke Paireepinart rabidpoobear at gmail.com
Thu Oct 19 02:59:22 CEST 2006


Chris Hengge wrote:
> The point of this script is to read a text file that has a bunch of 
> code that calls external files. Then read the directory where all the 
> files are.. I build a list from the text file (this works correct), 
> and I build a list from the directory (this works correct). I want to 
> print out the result of a match..
>
> Here is sample output.
> Tested:      Textname:       DirectoryName:
> Match!         a.exe             a.exe
> Fail!             A.exe            a.exe
ok, so you have a text file of filenames.
f = file('test.txt','r')
textnames = f.readlines()
f.close()

and you have a directory listing
import os
dirnames = os.listdir('.')

what you want to do is find matches.
so,

for item1 in textnames:
    for item2 in dirnames:
       if item1 == item2:
          print "Match!   %s and %s" % (item1, item2)

Why even try to use the 'in' syntax if it's not working correctly?
Maybe I still don't understand.
-Luke


More information about the Tutor mailing list