PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python » File Handling » Python Count Number of Lines in a File

Python Count Number of Lines in a File

Updated on: July 2, 2021 | 1 Comment

If the file is significantly large (in GB), and you don’t want to read the whole file to get the line count, This article lets you know how to get the count of lines present in a file in Python.

Table of contents

  • Steps to Get Line Count in a File
  • Generator and Raw Interface to get Line Count
  • Use readlines() to get Line Count
  • Use Loop and Sum Function to Count Lines
  • The in Operator and Loop to get Line Count
  • Count number of lines in a file Excluding Blank Lines
  • Conclusion

Steps to Get Line Count in a File

Count Number of Lines in a text File in Python

  1. Open file in Read Mode

    To open a file pass file path and access mode r to the open() function.
    For example, fp= open(r'File_Path', 'r') to read a file.

  2. Use for loop with enumerate() function to get a line and its number.

    The enumerate() function adds a counter to an iterable and returns it in enumerate object. Pass the file pointer returned by the open() function to the enumerate(). The enumerate() function adds a counter to each line.
    We can use this enumerate object with a loop to access the line number. Return counter when the line ends.

  3. Close file after completing the read operation

    We need to make sure that the file will be closed properly after completing the file operation. Use fp.close() to close a file.

Example

Consider a file “read_demo.txt.” See an image to view the file’s content for reference.

text file
text file
# open file in read mode
with open(r"E:\demos\files\read_demo.txt", 'r') as fp:
    for count, line in enumerate(fp):
        pass
print('Total Lines', count + 1)

Output:

Total Lines 8
  • The enumerate() function adds a counter to each line.
  • Using enumerate, we are not using unnecessary memory. It is helpful if the file size is large.
  • 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.

Generator and Raw Interface to get Line Count

A fast and compact solution to getting line count could be a generator expression. If the file contains a vast number of lines (like file size in GB), you should use the generator for speed.

This solution accepts file pointer and line count. To get a faster solution, use the unbuffered (raw) interface, using byte arrays, and making your own buffering.

def _count_generator(reader):
    b = reader(1024 * 1024)
    while b:
        yield b
        b = reader(1024 * 1024)

with open(r'E:\demos\files\read_demo.txt', 'rb') as fp:
    c_generator = _count_generator(fp.raw.read)
    # count each \n
    count = sum(buffer.count(b'\n') for buffer in c_generator)
    print('Total lines:', count + 1)

Output:

Total lines: 8

Use readlines() to get Line Count

If your file size is small and you are not concerned with performance, then the readlines() method is best suited.

This is the most straightforward way to count the number of lines in a text file in Python.

  • The readlines() method reads all lines from a file and stores it in a list.
  • Next, use the len() function to find the length of the list which is nothing but total lines present in a file.

Open a file and use the readlines() method on file pointer to read all lines.

Example:

with open(r"E:\demos\files\read_demo.txt", 'r') as fp:
    x = len(fp.readlines())
    print('Total lines:', x) # 8

Note: This isn’t memory-efficient because it loads the entire file in memory. It is the most significant disadvantage if you are working with large files whose size is in GB.

Use Loop and Sum Function to Count Lines

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.

with open(r"E:\demos\files\read_demo.txt", 'r') as fp:
    num_lines = sum(1 for line in fp)
    print('Total lines:', num_lines) # 8

If you want to exclude the empty lines count use the below example.

with open(r"E:\demos\files\read_demo.txt", 'r') as fp:
    num_lines = sum(1 for line in fp if line.rstrip())
    print('Total lines:', num_lines)  # 8

The in Operator and Loop to get Line Count

Using in operator and loop, we can get a line count of nonempty lines in the file.

  • Set counter to zero
  • Use a for-loop to read each line of a file, and if the line is nonempty, increase line count by 1

Example:

# open file in read mode
with open(r"E:\demos\files_demos\read_demo.txt", 'r') as fp:
    count = 0
    for line in fp:
        if line != "\n":
            count += 1
print('Total Lines', count)

Count number of lines in a file Excluding Blank Lines

For example, below is the text file which uses the blank lines used to separate blocks.

Jessa = 70
Kelly = 80
Roy  = 90

Emma = 25
Nat = 80
Sam = 75

When we use all the above approaches, they also count the blank lines. In this example, we will see how to count the number of lines in a file, excluding blank lines

Example:

count = 0
with open('read_demo.txt') as fp:
    for line in fp:
        if line.strip():
            count += 1

print('number of non-blank lines', count)

Output:

number of non-blank lines 6

Conclusion

  • Use readlines() or A loop solution if the file size is small.
  • Use Generator and Raw interface to get line count if you are working with large files.
  • Use a loop and enumerate() for large files because we don’t need to load the entire file in memory.

Filed Under: Python, Python File Handling

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Related Tutorial Topics:

Python Python File Handling

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Posted In

Python Python File Handling
TweetF  sharein  shareP  Pin

Python File Handling

  • File Handling In Python
  • Create File in Python
  • Open a File in Python
  • Read File in Python
  • Write to File in Python
  • Python File Seek
  • Rename Files in Python
  • Delete Files in Python
  • Copy Files in Python
  • Move Files in Python
  • List Files in a Directory
  • File Handling Quiz

All Python Topics

Python Basics Python Exercises Python Quizzes Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2023 pynative.com