Skip to content
Advertisement

Read in all csv files from a directory using Python

I hope this is not trivial but I am wondering the following:

If I have a specific folder with n csv files, how could I iteratively read all of them, one at a time, and perform some calculations on their values?

For a single file, for example, I do something like this and perform some calculations on the x array:

JavaScript

I know that I can check how many csv files there are in a given folder (check here):

JavaScript

But I failed to figure out how to possibly nest the numpy.genfromtxt() function in a for loop, so that I read in all the csv files of a directory that it is up to me to specify.

EDIT

The folder I have only has jpg and csv files. The latter are named eventX.csv, where X ranges from 1 to 50. The for loop I am referring to should therefore consider the file names the way they are.

Advertisement

Answer

That’s how I’d do it:

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