pythoncsvread-data

Saving Multiple files in Python


I am trying to create a new file each time the following runs. At the moment it creates 1 file and just overwrites it. Is there a to make it not overwrite and create a new file for each loop?

import xml.etree.ElementTree as ET
import time
import csv

with open('OrderCSV.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        orders_data = ET.Element('orders_data')
        orders = ET.SubElement(orders_data, 'orders')

        ##Order Details
        order_reference = ET.SubElement(orders, 'order reference')
        order_reference.set('',"12345")
        order_date = ET.SubElement(order_reference, 'order_date')
        order_priority  = ET.SubElement(order_reference, 'order_priority')
        order_category = ET.SubElement(order_reference, 'order_category')
        delivery_service = ET.SubElement(order_reference, 'delivery_service')
        delivery_service.text = row['delivery_service']

        timestr = time.strftime("%Y%m%d%H%M%S")

        mydata = ET.tostring(orders_data)
        myfile = open(timestr, "wb")
        myfile.write(mydata)

Solution

  • You could see if the file already exists and wait a bit

        while True:
            timestr = time.strftime("%Y%m%d%H%M%S")
            if not os.path.exists(timestr):
                break
            time.sleep(.1)
        with open(timestr, "wb") as myfile:
            mydata = ET.tostring(orders_data)
            myfile.write(mydata)
    

    Instead of waiting you could just add seconds. This will cause the file names to drift forward in time if you process a lot of them per second.

        mytime = time.time()
        while True:
            timestr = time.strftime("%Y%m%d%H%M%S", time.localtime(mytime))
            if not os.path.exists(timestr):
                break
            time.sleep(.1)
        with open(timestr, "wb") as myfile:
            mydata = ET.tostring(orders_data)
            myfile.write(mydata)
    

    Another option is to get a single timestamp before the loop and update it as you go.

    mytime = time.strftime("%Y%m%d%H%M%S")
    for index, row in enumerate(reader):
         ....
         mytime = f"mytime-{index}"
         ....