Skip to content
Advertisement

Finding the number of rows for all files within a folder

Hello I am trying to find the number of rows for all files within a folder. I am trying to do this for a folder that contains only “.txt” files and for a folder that contains .”csv” files.

I know that the way to get the number of rows for a SINGLE “.txt” file is something like this:

JavaScript

Whereas for a SINGLE “.csv” file is something like this:

JavaScript

But how can I do this for ALL files within a folder? That is, how can I loop each of these procedures across all files within a folder and, ideally, export the output into an excel sheet with columns akin to these:

JavaScript

and so on and so on.

Thank you so much for your help.

Advertisement

Answer

You can use glob to detect the files and then just iterate over them.

Other methods : How do I list all files of a directory?

JavaScript

Then, if you want to export this result in a .csv or .xslx file, I recommend using pandas.

JavaScript

You can also use pandas.ExcelWriter() if you want to use the .xlsx format. Link to documentation & examples : Pandas – ExcelWriter doc

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement