I have many files in a folder. I open the folder and import all the data from all the files into my model. Here My program imports data into one file at a time, so it will read all the data in the file once and I repeat every line at the end of each line. I save a transaction in a database using obj.save() .
Here, I encountered some exceptions, while import may introduce a mismatch error, so the program stops importing this file, and half of the data will be imported and move to the next file. So this problem causes duplicate data, so here I need to save the transaction using the obj.save() file, if there is no exception, how can I achieve this, can someone 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))
source share