## Question

Asked By – GrantU

I’m using python (Django Framework) to read a CSV file. I pull just 2 lines out of this CSV as you can see. What I have been trying to do is store in a variable the total number of rows the CSV also.

**How can I get the total number of rows?**

```
file = object.myfilePath
fileObject = csv.reader(file)
for i in range(2):
data.append(fileObject.next())
```

I have tried:

```
len(fileObject)
fileObject.length
```

**Now we will see solution for issue: How to obtain the total numbers of rows from a CSV file in Python? **

## Answer

You need to count the number of rows:

```
row_count = sum(1 for row in fileObject) # fileObject is your csv.reader
```

Using `sum()`

with a generator expression makes for an efficient counter, avoiding storing the whole file in memory.

If you already read 2 rows to start with, then you need to add those 2 rows to your total; rows that have already been read are not being counted.

This question is answered By – Martijn Pieters