Python 3 Script to Count Number of Lines or Rows in CSV File Using CSV Module Full Project For Beginners

Python 3 Script to Count Number of Lines or Rows in CSV File Using CSV Module Full Project For Beginners

 

Welcome folks today in this blog post we will be counting number of lines or rows in csv file using csv module.All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to make an app.py file and copy paste the following code

 

app.py

 

import csv
file = open("sample.csv")
reader = csv.reader(file)
lines= len(list(reader))

print(lines)

 

 

Now if you execute the python script by typing the below command as shown below

 

python app.py

 

 

Using For Loop

 

num_rows = 0

for row in open("sample.csv"):
    num_rows += 1

print(num_rows)

Leave a Reply