0

I have a directory where there are multiple csv files. Currently I am able to read all the files sequentially using for loop and display their contents. I need to to write the contents from all the csv files sequentially into a new csv file but I am missing something as in my new csv has no data in it.

this is what I am doing :

import os
import csv     

path = r'C:\Users\hu170f\Documents\WORK\MAAP_FILE_DB_REQ\\' 
fileNames = os.listdir(path)

for f in fileNames:
    file = open(path+f)
    csvreader = csv.reader(file)
    
    rows = []
    for row in csvreader:
        rows.append(row)
      
    for i in rows:
        print(i)
        #OFile = open('C:\Users\hu170f\Documents\WORK\MAAP_FILE_DB_REQ\ALL_DATA.csv','w')
        writer = csv.writer(open('C:\Users\hu170f\Documents\WORK\MAAP_FILE_DB_REQ\ALL_DATA.csv', 'wb'))
        #for row in csvreader:
         #   row1 = csvreader.next()
        writer.writerow(i)

1 Answer 1

2

You are overwriting the file each row you try to write. Using the w argument for the open method will overwrite existing files.

The argument you need to use in the case you want to append to files (or create new files if non-existing) is a

See Python File Open for more informations about python file modes.

import os
import csv     

path = r'C:\Users\hu170f\Documents\WORK\MAAP_FILE_DB_REQ' 
fileNames = os.listdir(path)

with open('C:\Users\hu170f\Documents\WORK\MAAP_FILE_DB_REQ\ALL_DATA.csv', 'a') as output:    
    writer = csv.writer(output)
    for f in fileNames:
        with open(os.path.join(path, f), "r") as file:
            csvreader = csv.reader(file)
            for row in csvreader:
                print(row)
                writer.writerow(row)

If the csv files have the same columns and formats you could also simply copy the first file and append the others, excluding their headers.

import os
import shutil

path = r'C:\Users\hu170f\Documents\WORK\MAAP_FILE_DB_REQ'
fileNames = os.listdir(path)
output = r'C:\Users\hu170f\Documents\WORK\MAAP_FILE_DB_REQ\ALL_DATA.csv'

# Copy the first file:
shutil.copyfile(os.path.join(path,fileNames[0]), output)

# Append the remaining file contents, excluding each first line
with open(output, 'a') as out:
    for file in fileNames[1:]:
        with open(os.path.join(path, file), 'r') as in_:
            out.write(''.join(in_.readlines()[1:]))
Sign up to request clarification or add additional context in comments.

4 Comments

it is showing error for line "with open(os.path.join(path), file), 'r')" stating as invalid syntax and if I add opening braces before os.path then the error changes to No directory present for ALL_DATA
@shikha Sorry, there was a typo. The ) after path needs to be removed. I edited the answer
getting error as No such file or directory: 'ALL_DATA.csv' even though the file exist.
@shikha I changed the example to use an absolute path. I tested it while I was in the directory. shutil.copyfile takes either an absolute or relative path. Furthermore I found another problem in the out.write line while testing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.