*very new to Python I need to merge a large number of txt files from a single directory into one csv file. The text from the files needs to be converted into separate rows and columns (five columns in each file, N number of rows). I used the code from this question:
import os
import csv
dirpath = 'path_of_directory'
output = 'output_file.csv'
with open(output, 'w') as outfile:
csvout = csv.writer(outfile)
csvout.writerow(['FileName', 'Content'])
files = os.listdir(dirpath)
for filename in files:
with open(dirpath + '/' + filename) as afile:
csvout.writerow([filename, afile.read()])
afile.close()
outfile.close()
It works for me but it puts all the content from the file into one table cell. I read through a lot of Q&As and couldn't figure out how to modify the code in order to separate the content into different columns and rows.