0
votes

I have a piece of python code which is supposed to open (or create) a CSV file and append a new row to the end of it. However, there is an extra blank line added between each entry. Is there a way to avoid this?

I have around 500 instances of the script which all access the same file, though (in theory) they should access the file at different times.

def writeBest(fileName, savePath, data):

    # Create a filename
    name = "BEST_" + fileName + ".csv"

    # Create the complete filename including the absolute path 
    completePath = os.path.join(savePath, fileName)

    # Check if directory exists
    if not os.path.exists(completePath):
        os.makedirs(completePath)

    completeName = os.path.join(completePath, name)

    # Write the data to a file
    theFile = open(completeName, 'wb')
    writer = csv.writer(theFile, quoting=csv.QUOTE_ALL)
    writer.writerow(data)

    # Close the file
    theFile.close()
2
What is data? which type? - Tom Ron
If you want to add to the csv file I'd imagine you want to open it with the ab mode, not wb. - Holloway
it needs to create the file if it doesnt exist - Chris Headleand
@ChrisHeadleand The 'a' mode will create the file if it doesn't exist. (Test it yourself.) Anyway, can you show us a same of your data argument? - 2rs2ts
@ChrisHeadleand the w will overwrite a file if it already exists rather than add to it. - Holloway

2 Answers

3
votes

Problem answered through comments. The issue was using the wb mode not ab mode.