how to count the total number of lines in a text file using python
You can use sum() with a generator expression: with open(‘data.txt’) as f: print sum(1 for _ in f) Note that you cannot use len(f), since f is an iterator. _ is a special variable name for throwaway variables, see What is the purpose of the single underscore “_” variable in Python?. You can use len(f.readlines()), … Read more