Write a Python Program to Get Line Count of a File

In Python, there are several ways to get the line count of a file.

One simple way is to use the built-in len() function and the readlines() method.

Here’s an example program that reads a file and counts the number of lines:

# Open the file in read mode
file = open("filename.txt", "r")

# Use readlines() method to read all the lines
lines = file.readlines()

# Use len() function to get the number of lines
num_lines = len(lines)

# Print the result
print("Number of lines in the file:", num_lines)

# Close the file
file.close()

In this program, we first open the file in read mode using the open() function.

Then, we use the readlines() method to read all the lines of the file and store them in the lines variable.

Finally, we use the len() function to get the number of lines in the file and print it to the console.

It’s important to note that the readlines() method reads the entire file into memory at once, which can be a problem for large files.

If you’re working with large files, you may want to use a more memory-efficient approach, such as reading the file line by line using a for loop and counting the lines as you go.

Here’s an example program that reads a file line by line and counts the number of lines:

# Open the file in read mode
file = open("filename.txt", "r")

# Initialize the line count to 0
num_lines = 0

# Use a for loop to iterate over each line
for line in file:
    # Increment the line count for each line
    num_lines += 1

# Print the result
print("Number of lines in the file:", num_lines)

# Close the file
file.close()

In this program, we open the file in read mode and initialize the line count to 0.

Then, we use a for loop to iterate over each line of the file. For each line, we increment the line count by 1.

Finally, we print the number of lines to the console and close the file.

Using these two methods, you can easily get the line count of a file in Python.

Remember to always close the file after you’re done reading from it to avoid memory leaks.

Alternatively, you can also use the with statement to open and read the file.

The advantage of using the with statement is that it automatically closes the file for you, even if an exception occurs.

Here’s an example program that uses the with statement to get the line count of a file:

# Open the file in read mode using the with statement
with open("filename.txt", "r") as file:
    # Use readlines() method to read all the lines
    lines = file.readlines()

# Use len() function to get the number of lines
num_lines = len(lines)

# Print the result
print("Number of lines in the file:", num_lines)

In this program, we use the with statement to open the file in read mode.

Inside the with block, we use the readlines() method to read all the lines of the file and store them in the lines variable.

Then, we use the len() function to get the number of lines in the file and print it to the console.

Finally, the with statement automatically closes the file for us.


In conclusion, there are multiple ways to get the line count of a file in Python, including using the readlines() method, reading the file line by line using a for loop, and using the with statement.

Choose the method that works best for your specific use case and always remember to close the file after you’re done reading from it.