I have this code that should open and read two text files, and match when a word is present in both. The match is represented by printing "SUCESS" and by writing the word to a temp.txt file.
dir = open('listac.txt','r')
path = open('paths.txt','r')
paths = path.readlines()
paths_size = len(paths)
matches = open('temp.txt','w')
dirs = dir.readlines()
for pline in range(0,len(paths)):
for dline in range(0,len(dirs)):
p = paths[pline].rstrip('\n').split(".")[0].replace(" ", "")
dd = dirs[dline].rstrip('\n').replace(" ", "")
#print p.lower()
#print dd.lower()
if (p.lower() == dd.lower()):
print "SUCCESS\n"
matches.write(str(p).lower() + '\n')
listac.txt is formatted as
/teetetet
/eteasdsa
/asdasdfsa
/asdsafads
.
.
...etc
paths.txt is formated as
/asdadasd.php/asdadas/asdad/asd
/adadad.html/asdadals/asdsa/asd
.
.
...etc
hence I use the split function in order to get the first /asadasda (within paths.txt) before the dot. The problem is that the words never seem to match, I have even printed out each comparison before each IF statement and they are equal, is there something else that Python does before comparing strings?
=======
Thanks everyone for the help. As suggested by you, I cleaned the code so It ended up like this:
dir = open('listac.txt','r')
path = open('paths.txt','r')
#paths = path.readlines()
#paths_size = len(paths)
for line in path:
p = line.rstrip().split(".")[0].replace(" ", "")
for lines in dir:
d = str(lines.rstrip())
if p == d:
print p + " = " + d
Apparently, having p declared and initialized before entering the second for loop makes a difference in the comparison down the road. When I declared p and d within the second for loop, it wouldn't work. I don't know the reason for that but If someone does, I am listening :)
Thanks again!
for pline in range(0,len(paths)), just iterate over the elements withfor pline in paths. And whyrstrip('\n'). There might be an extra\r. Just userstrip().p = ...outside the inner for loop since it's doing the same calculation each time.