Write to CSV File

Writes a line of CSV to a file
If the file specified does not already exists, it will be created
Otherwise the file will be appended to include the new line
Accepts a path to a CSV file, and a CSV line data.

def updateCSV(aFilePath, lineData):
    aFolder, aFile = os.path.split(aFilePath)
    if os.path.exists(aFilePath):
        with open(aFilePath, "a+", newline='') as csvfile:
            fieldnames = lineData.keys()
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writerow(lineData)
    else:
        if not os.path.exists(aFolder):
            os.makedirs(aFolder)
        with open(aFilePath, "x", newline='') as csvfile:
            fieldnames = lineData.keys()
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()