How to read hundreds of csv files from a directory and sum a specific column from each of them?
Question: I have around 650 csv files in a directory. Each of them have three columns
with header(#ch# ##kev## ###count###), and 2050 rows.One of the file image is
shown here partly.

    
    
        ch       kev     count
         0        0        0
         1        0        0
         2        0        0
         .        .        .
         .        .        .  
         .        .        .
        100       0        30
        101       0        70
         .        .        .
         .        .        .
        200       .        1000
        201       0        1037
         .        .        .
         .        .        .
        2050      0        0        
    

I would like to sum of column(###count###) for a specific range (100 -200) of
value of column(#ch#). I am able to write the program only for a single csv
file is show below:

    
    
        import csv
    
        cr = csv.reader(open("D:\\Pythontest\\test1.csv", 'r'))
        cr.next()
        ['ch', 'kev', 'count']
    
        total = 0
    
        for row in cr:
            if 100 <= int(row[0]) <= 200:
               total += int(row[2])
    
        print total
    

But I don't know how to write the program for all of the files at a time. I
would like to extract sum value from each of the file and put them together in
a separate file. I am using Python 2.7.


Answer: Since you have a working program for one file all you need is a way to use
that program on each file. I suggest doing the following:

  * Learn how to use the glob module to list all the csv files, <http://pymotw.com/2/glob/>
  * Move your code to a function, <http://www.tutorialspoint.com/python/python_functions.htm>, taking the filename as an argument and returning the total for that file.
  * Loop over each file that glob gives you and run the function, adding up the total as you go.

Good luck and if you run into a problem with one of the steps feel free to
post a new question that's more specific.

