# Find the length of a file infile = open('filename.txt','r') nlines = len(infile.readlines()) infile.seek(0)