import pickle import os List = [] for fileName in os.listdir ( '/test/' ): Directory = fileName List.append(Directory) print Directory List.sort() print List ListLength = len(List) - 1 x=0 y=5 while y <= 3 while x < ListLength: b = List[x] c = List[x + 1] backward1 = List[x - 1] d = b[:3] e = c[:3] backward2 = backward1[:3] f = str(d) g = str(e) backward3 = str(backward2) if f==g: print "Yes" print "Match",b,"and",c else: print "No" print b,"and",c,"no match" if f==backward3: print "But the last two matched" print b,"and",backward1 else: print "The last two did not match" x = x + 1