I have many files in a folder I am open the folder and import all the data inside all the files into my model, Here My program import data's in one file at a time,so it will read all data in the file once and I iterate through each line at the end of each line I save the transaction to database using obj.save()
.
Here I am facing some exception while import may type mismatch error so the program stop import of that file and half of the data will be imported and it move to next file. So this problem cause duplicate data so here I need save the transaction using obj.save()
of a file if there is no exception how can i achieve this can any one help me
fromFile=open(path)
for eachLine in fromFile:
obj = SAMP()
fieldsInline = eachLine.split(",")
n=len(fieldsInline)
if lines!=1:
#obj.dates = dates
obj.col1 = fieldsInline[0].strip()
obj.col2 = fieldsInline[1].strip()
obj.col3 = fieldsInline[2].strip()
obj.col4 = fieldsInline[3].strip()
obj.save()
lines+=1
except BaseException as e:
logging.info('\tError in importing %s line %d : %s' % (path, lines, e.__str__()))
else:
logging.info("\tImported %s, %d lines" % (path, lines))