0

this is my first post :). I have to check that each csv contains data - hence that the number of rows > 1 (accounting for header).

I am therefor trying to append to a dic the

  • name of the file
  • number of rows

So far, I get the name of the file in my dict but not the number of rows and instead the length of the path + file name

import glob
import csv

all_files = glob.glob(path + '*.csv')

dic = {}

for file in all_files:
    dic[file] = len(list(csv.reader(file)))

Thank you all very much!

Arnekowski
  • 11
  • 2

1 Answers1

1

I think this question was answered already here as @Metapod commented on your question.

In short, you should use sum() method for counting the number of lines efficiently:

for file in all_files:
    dic[file] = sum(1 for row in(csv.reader(file)))
pugi
  • 323
  • 1
  • 12