ajjohnson190
ajjohnson190

Reputation: 513

Python csv writer only writes header and one line

I'm using DictWriter to write and append csv files. When I write to my working directory the csv writes and appends correctly, but when I have the file location set to my network drive, it only writes the header and the first line.

def collectSerials(salesOrderNum, shipDate, info, name, warranty):

    while 1:
        sn = input("Scan in the item's serial number:\n")

        #write to csv
        #salesOrderNum does not change
        #Checks to see if file already exists
        if (path.exists(salesOrderNum+'.csv')):
            #if so append
            with open('//ibl1/info/SalesOrders/'+salesOrderNum+'.csv', 'a', newline='') as csvfile:
                fieldnames = ['Product Name', 'Serial Number', 'Shipping Date', 'Warranty Expiration']
                writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                writer.writerow({'Product Name': name, 'Serial Number': sn, 'Shipping Date': shipDate, 'Warranty Expiration': warranty})
        else:
            #if not create new file
            with open('//ibl1/info/SalesOrders/'+salesOrderNum+'.csv', 'w', newline='') as csvfile:
                fieldnames = ['Product Name', 'Serial Number', 'Shipping Date', 'Warranty Expiration']
                writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                writer.writeheader()
                writer.writerow({'Product Name': name, 'Serial Number': sn, 'Shipping Date': shipDate, 'Warranty Expiration': warranty})

It's weird that it writes only the header and the first line inputted in the loop. I would expect it to just not work at all if there was an issue.

Upvotes: 0

Views: 348

Answers (1)

InTheEconomix
InTheEconomix

Reputation: 46

Its because your os exists is pointing to your working directory. You are only checking to see if the file exists locally, then writing a new file to your network drive.

replace

if (path.exists(salesOrderNum+'.csv')):

with

if (path.exists('//ibl1/info/SalesOrders/'+salesOrderNum+'.csv')):

Upvotes: 2

Related Questions