How to count the number of lines in a CSV file in Python?
Last Updated :
20 May, 2025
Counting the number of lines in a CSV file in Python means determining how many rows the file contains, including or excluding the header depending on your needs. For example, if your CSV file has 100 data rows plus one header row, the total line count is 101. We will use the following dataset to demonstrate these operations:
Python
import pandas as pd
res = pd.read_csv('Data.csv')
print(res)
Output

Using sum()
This method reads the file line by line and counts each one. It’s a pure Python approach, very fast and memory efficient and it works even if the file is huge.
Python
with open('Data.csv', 'r', encoding='utf-8') as f:
res = sum(1 for line in f)
print(res)
Output
11
Explanation: Inside the with block, a generator expression iterates over each line in the file (for line in f) and yields 1 for every line. The sum() function then adds all those 1s, effectively counting the number of lines in the file.
Using wc-l
wc -l is a Unix command that counts the number of lines in a file. Here, we run that command from Python using subprocess.
Python
import subprocess
res = int(subprocess.check_output(['wc', '-l', 'Data.csv']).split()[0])
print(res)
Output
11
Explanation: subprocess.check_output runs the command wc -l Data.csv, which counts the number of lines in the file. The output is split into parts and the first part (the line count) is converted to an integer and stored in res.
Using Pandas with chunksize
This is the simplest and most straightforward way to count the number of rows. You load the whole CSV file into a pandas DataFrame and count how many rows it has.
Python
import pandas as pd
res = sum(len(chunk) for chunk in pd.read_csv('Data.csv', chunksize=10000))
print(res + 1) # data rows + header
Output
11
Explanation: pd.read_csv('Data.csv', chunksize=10000) reads the CSV in chunks of up to 10,000 rows. len(chunk) counts rows per chunk and sum(...) totals all data rows (excluding the header). Adding 1 accounts for the header, so the final output is the total lines in the CSV.
Using len(pd.read_csv(...))
This is the simplest and most straightforward way to count the number of rows. You load the whole CSV file into a pandas DataFrame and count how many rows it has.
Python
import pandas as pd
df = pd.read_csv('Data.csv')
print(len(df) + 1) # data rows + header
Output
11
Explanation: pd.read_csv('Data.csv') loads the CSV into a DataFrame df. len(df) counts only the data rows, excluding the header. Adding +1 includes the header row, so len(df) + 1 gives the total number of lines in the CSV file.
Related articles
Similar Reads
Count number of lines in a text file in Python Counting the number of characters is important because almost all the text boxes that rely on user input have a certain limit on the number of characters that can be inserted. For example, If the file is small, you can use readlines() or a loop approach in Python. Input: line 1 line 2 line 3 Output:
3 min read
How to count the number of pages in a PDF file in Python In this article, we will see how can we count the total number of pages in a PDF file in Python, For this article there is no such prerequisite, we will use PyPDF2 library for this purpose. PyPDF2 is a free and open-source pure-Python PyPDF library capable of performing many tasks like splitting, me
4 min read
How to read numbers in CSV files in Python? Prerequisites: Reading and Writing data in CSV, Creating CSV files CSV is a Comma-Separated Values file, which allows plain-text data to be saved in a tabular format. These files are stored in our system with a .csv extension. CSV files differ from other spreadsheet file types (like Microsoft Excel
4 min read
How to Count number of Lines in a Git Repository? Counting the number of lines in a Git repository can be useful for various reasons, such as understanding the size of the project or monitoring codebase growth over time. There are several methods to achieve this, including using Git command line tools, the CLOC (Count Lines of Code) tool, and custo
4 min read
How to Count the Number of Rows in a MySQL Table in Python? MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a
2 min read
Count the Number of Null Elements in a List in Python In data analysis and data processing, It's important to know about Counting the Number of Null Elements. In this article, we'll explore how to count null elements in a list in Python, along with three simple examples to illustrate the concept. Count the Number of Null Elements in a List in PythonIn
3 min read