0

I need to read each csv file from a predefined dir, for each csv in that dir I need to take each row and write it to a new csv file. Currently, I have this code snippet that reads a specific csv file and loops on each row.

import csv
with open('E:\EE\EE\TocsData\CSAT\csat_20140331.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='|')
    for row in reader:
        # write row to a seperate csv file
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
user3628777
  • 529
  • 3
  • 10
  • 20
  • Using unescaped backslashes in filenames is dangerous, see http://stackoverflow.com/q/23529312/1258041 – Lev Levitsky May 18 '14 at 13:49
  • Duplicate of [How do I read and write CSV files with Python?](http://stackoverflow.com/q/41585078/562769) and [Find all files in directory with extension .txt in Python](http://stackoverflow.com/q/3964681/562769) – Martin Thoma Jan 11 '17 at 07:59

2 Answers2

0

Something similar to this should work. Loop through all files in the directory, read from each and write all rows to the new file.

    myDirectory = "E:\EE\EE\TocsData\CSAT\\"
    myNewCSV= "E:\myNewCSV.csv"
    with open(myNewCSV,'ab') as w:  #append bytes:'ab', write bytes:'wb' - whichever you want.
        for myFile in os.listdir(myDirectory):
            absolutePath = myDirectory + str(myFile)
            writer = csv.writer(w)
            with open(absolutePath, 'rb') as r:
                 reader = csv.reader(r)
                 for row in reader:
                     writer.writerow(row)
Nate Jenson
  • 2,664
  • 1
  • 25
  • 34
  • 1
    This is great, but you can open both files in the same context: `with open(absolutepath,'rb') as r, open(myNewCSV, 'wb') as w:`. Also, consider using raw strings for the paths (or use forward slashes, which also work in Windows). – Burhan Khalid May 18 '14 at 14:50
0

Try this:

import glob
import csv

with open('somefile.out', 'wb') as out:
    writer = csv.writer(out, delimiter=',')
    for inf in glob.glob(r'E:\EE\EE\TocsData\CSAT\*.csv'):
        with open(inf, 'rb') as csv_input:
            reader = csv.reader(csv_input, delimiter=',', quotechar='|')
            for row in reader:
                writer.write(row)
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284