def One(): duble = [] for l in open("log.log"): duble.append(l) for i in set(duble): f = Two(i.rstrip()) def Two(*args): for fname in glob.glob('test*.txt'): for line in open(fname): if args[0]==line: retrun line The question is how in the Two () function to read all files once and find what you need in the read, and such an algorithm works, for each line it reads all files every time.