import csv
shop_products=' '
total_price=0.00
RECIEPT_FILE=open('reciept.txt', 'wt')
while True:
GTIN_code=input('Please enter the GTIN-8 code of the product that you would like to purchase or simply press the enter key if you do not')
if GTIN_code == '':
break
shop_products=open('Product.csv', 'rt')
data_in_file=csv.reader(shop_products)
for row in data_in_file:
for field in row:
if field==GTIN_code:
quantity=int(input('How many of the products do you want to purchase'))
subtotal_price=float(row[2])*quantity
total_price=total_price+subtotal_price
RECIEPT_FILE.write(row[0]+' '+row[1]+' '+str(quantity)+' '+'£'+row[2])
RECIEPT_FILE.close()
RECIEPT_FILE.write('Your total price of all your purchases is '+str(total_price))
RECIEPT_FILE.close()
这里我试图通过将GTIN-8代码写入文本文件来制作收据,但每当我运行程序时,它都会报告此错误:
Traceback (most recent call last):
File "C:\Users\Ahmed\Documents\Task 2 test code\Products reciept generator - Copy.pyw", line 20, in <module>
RECIEPT_FILE.write(row[0]+' '+row[1]+' '+str(quantity)+' '+'£'+row[2])
ValueError: I/O operation on closed file.
我在python 3.2中工作。
答案 0 :(得分:1)
查看循环的最后一行,从底部开始第三行:第一次写入时关闭文件。你下一次写的是非法的。
删除该行;让程序的最后一行执行您的操作,只有关闭。