num_lines = sum(1 for line in open('myfile.txt')) # Notice: Problem with this solution is that it won't # count empty line if it is at end of file.