
With open(r"E:\demos\files_demos\read_demo.txt", 'r') as fp: Use a for-loop to read each line of a file, and if the line is nonempty, increase line count by 1.Using in operator and loop, we can get a line count of nonempty lines in the file. Print('Total lines:', num_lines) # 8 The in Operator and Loop to get Line Count Num_lines = sum(1 for line in fp if line.rstrip()) with open(r"E:\demos\files\read_demo.txt", 'r') as fp: If you want to exclude the empty lines count use the below example. You can use the for loop to read each line and pass for loop to sum function to get the total iteration count which is nothing but a line count. It is the most significant disadvantage if you are working with large files whose size is in GB. Note: This isn’t memory-efficient because it loads the entire file in memory. Open a file and use the readlines() method on file pointer to read all lines.Įxample: with open(r"E:\demos\files\read_demo.txt", 'r') as fp: Next, use the len() function to find the length of the list which is nothing but total lines present in a file.The readlines() method reads all lines from a file and stores it in a list.This is the most straightforward way to count the number of lines in a text file in Python. If your file size is small and you are not concerned with performance, then the readlines() method is best suited. Output: Total lines: 8 Use readlines() to get Line Count

With open(r'E:\demos\files\read_demo.txt', 'rb') as fp:Ĭ_generator = _count_generator(fp.raw.read)Ĭount = sum(unt(b'\n') for buffer in c_generator) To get a faster solution, use the unbuffered (raw) interface, using byte arrays, and making your own buffering. This solution accepts file pointer and line count.
#Gzip file peek python generator
If the file contains a vast number of lines (like file size in GB), you should use the generator for speed. Generator and Raw Interface to get Line CountĪ fast and compact solution to getting line count could be a generator expression. Note: enumerate(file_pointer) doesn’t load the entire file in memory, so this is an efficient fasted way to count lines in a file.Using enumerate, we are not using unnecessary memory.The enumerate() function adds a counter to each line.It does not store any personal data.With open(r"E:\demos\files\read_demo.txt", 'r') as fp: The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. The cookie is used to store the user consent for the cookies in the category "Performance". This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. The cookies is used to store the user consent for the cookies in the category "Necessary".

The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".

The cookie is used to store the user consent for the cookies in the category "Analytics". These cookies ensure basic functionalities and security features of the website, anonymously. Necessary cookies are absolutely essential for the website to function properly.
