I am reading all the files from a given folder (contains Dir, Sub dir and files of type .csv, .txt ..)
I need to get the following information into an output file in the following format:
FileLocation, FileName, Delimiter, Columns
(All columns needed in a cell separated by delimiter)
I am using the following script which works fine except delimiter. I have tried using csv.sniffer but it does not work.
import sys,os,csv
ofilew = open('D:OutputFile/Columns_Info.csv', 'w')
ofile = open('D:OutputFile/Columns_Info.csv', 'a')
root = 'D:UnZipFiles'
path = os.path.join(root)
columninfo = 'FolderLocation, FileName, Delimiter, Columns' + '
'
ofilew.write(columninfo)
for r,d,f in os.walk(path):
for file in f:
fullfilepath = os.path.join(r,file)
with open(fullfilepath,'r') as f:
columninfo = f.readline()
columninfo = columninfo.replace(",", ";")
output = file +','+ columninfo
outputfinal = r + ',' + output
ofile.write(outputfinal)
See Question&Answers more detail:os