我正在使用DictWriter编写和附加csv文件。当我写入工作目录时,csv会正确写入和追加,但是当我将文件位置设置为网络驱动器时,它只会写入标题和第一行。
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})
很奇怪,它只写标题和循环中输入的第一行。如果出现问题,我希望它根本不起作用。
答案 0 :(得分:2)
这是因为您的操作系统存在指向您的工作目录。您只需要检查文件是否在本地,然后将新文件写入网络驱动器即可。
替换
if (path.exists(salesOrderNum+'.csv')):
使用
if (path.exists('//ibl1/info/SalesOrders/'+salesOrderNum+'.csv')):