# How to obtain the total numbers of rows from a CSV file in Python?

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
for i in range(2):
data.append(fileObject.next())
``````

I have tried:

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

## Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

### Solution 1

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.

### Solution 2

#### 2018-10-29 EDIT

Thank you for the comments.

I tested several kinds of code to get the number of lines in a csv file in terms of speed. The best method is below.

``````with open(filename) as f:
sum(1 for line in f)
``````

Here is the code tested.

``````import timeit
import csv
import pandas as pd

filename = './sample_submission.csv'

def talktime(filename, funcname, func):
print(f"# {funcname}")
t = timeit.timeit(f'{funcname}("{filename}")', setup=f'from __main__ import {funcname}', number = 100) / 100
print('Elapsed time : ', t)
print('n = ', func(filename))
print('\n')

def sum1forline(filename):
with open(filename) as f:
return sum(1 for line in f)
talktime(filename, 'sum1forline', sum1forline)

with open(filename) as f:

def lenpd(filename):
return len(pd.read_csv(filename)) + 1
talktime(filename, 'lenpd', lenpd)

cnt = 0
with open(filename) as f:
for row in cr:
cnt += 1
return cnt

def openenum(filename):
cnt = 0
with open(filename) as f:
for i, line in enumerate(f,1):
cnt += 1
return cnt
talktime(filename, 'openenum', openenum)
``````

The result was below.

``````# sum1forline
Elapsed time :  0.6327946722068599
n =  2528244

Elapsed time :  0.655304473598555
n =  2528244

# lenpd
Elapsed time :  0.7561274056295324
n =  2528244

Elapsed time :  1.5571560935772661
n =  2528244

# openenum
Elapsed time :  0.773000013928679
n =  2528244
``````

In conclusion, `sum(1 for line in f)` is fastest. But there might not be significant difference from `len(f.readlines())`.

`sample_submission.csv` is 30.2MB and has 31 million characters.

### Solution 3

To do it you need to have a bit of code like my example here:

``````file = open("Task1.csv")
print (numline)
``````

I hope this helps everyone.

### Solution 4

Several of the above suggestions count the number of LINES in the csv file. But some CSV files will contain quoted strings which themselves contain newline characters. MS CSV files usually delimit records with \r\n, but use \n alone within quoted strings.

For a file like this, counting lines of text (as delimited by newline) in the file will give too large a result. So for an accurate count you need to use csv.reader to read the records.

### Solution 5

First you have to open the file with open

``````input_file = open("nameOfFile.csv","r+")
``````

Then use the csv.reader for open the csv

``````reader_file = csv.reader(input_file)
``````

At the last, you can take the number of row with the instruction ‘len’

``````value = len(list(reader_file))
``````

The total code is this:

``````input_file = open("nameOfFile.csv","r+")
``````

Remember that if you want to reuse the csv file, you have to make a input_file.fseek(0), because when you use a list for the reader_file, it reads all file, and the pointer in the file change its position

### Solution 6

`row_count = sum(1 for line in open(filename))` worked for me.

Note : `sum(1 for line in csv.reader(filename))` seems to calculate the length of first line

### Solution 7

After iterating the whole file with `csv.reader()` method, you have the total number of lines read, via instance variable `line_num`:

``````import csv
with open('csv_path_file') as f:
for row in csv_reader:
pass
``````

Quoting the official documentation:

The number of lines read from the source iterator.

Small caveat:

• total number of lines, includes the header, if the CSV has.

### Solution 8

This works for csv and all files containing strings in Unix-based OSes:

``````import os

numOfLines = int(os.popen('wc -l < file.csv').read()[:-1])
``````

In case the csv file contains a fields row you can deduct one from `numOfLines` above:

``````numOfLines = numOfLines - 1
``````

### Solution 9

I think we can improve the best answer a little bit, I’m using:

``````len = sum(1 for _ in reader)
``````

Moreover, we shouldnt forget pythonic code not always have the best performance in the project. In example: If we can do more operations at the same time in the same data set Its better to do all in the same bucle instead make two or more pythonic bucles.

### Solution 10

``````numline = len(file_read.readlines())
``````

### Solution 11

You can also use a classic for loop:

``````import pandas as pd

count = 0
for i in df['a_column']:
count = count + 1

print(count)
``````

### Solution 12

``````import csv
count = 0
with open('filename.csv', 'rb') as count_file:
for row in csv_reader:
count += 1

print count
``````

### Solution 13

Use “list” to fit a more workably object.

You can then count, skip, mutate till your heart’s desire:

``````list(fileObject) #list values

len(list(fileObject)) # get length of file lines

list(fileObject)[10:] # skip first 10 lines
``````

### Solution 14

``````import pandas as pd
totalInstances=len(data)
``````

### Solution 15

might want to try something as simple as below in the command line:

``````sed -n '\$=' filename
``````

or

``````wc -l filename
``````

### Solution 16

If you have to parse the CSV (e.g., because of the presence of line breaks in the fields or commented out lines) but the CSV is too large to fit the memory all at once, you might parse the CSV piece-by-piece:

``````import pandas as pd
import os
import sys

csv.field_size_limit(sys.maxsize)  # increase the maximal line length in pd.read_csv()

cnt = 0
for chunk in pd.read_csv(filepath, chunksize=10**6):
cnt += len(chunk)
print(cnt)
``````

### Solution 17

I think mine will be the simplest approach here:

``````import csv
file = open(filename, 'r')
file.close
print("row", len(list(csvfile)))
``````

### Solution 18

try

``````data = pd.read_csv("data.csv")
data.shape
``````

and in the output you can see something like (aa,bb) where aa is the # of rows

### Solution 19

If you are working on a Unix system, the fastest method is the following shell command

``````cat FILE_NAME.CSV | wc -l
``````

From Jupyter Notebook or iPython, you can use it with a `!`:

``````! cat FILE_NAME.CSV | wc -l
``````

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0