python - I keep getting this error and I dont know hove to fix it -


i open file saying file not open. stuck on . new python.

here error:

traceback (most recent call last): file "\\users2\121721$\computer science\progamming\task3.py", line 43, in   <module> file.write(barcode + ":" + item + ":" + price + ":" +str(int((stocklevel-    float(howmany)))))     valueerror: i/o operation on closed file. 

here code:

#open file in read mode file = open("data.txt","r") #read each line of data vairble filedata = file.readlines() #close file file.close()  total = 0 #create vairble total anotheritem = "y" # create while anotheritem == "y" or anotheritem  == "y" or  anotheritem ==  "yes" or  anotheritem ==  "yes" or  anotheritem ==  "yes":         print("please enter barcode")         usersitem=input()         line in  filedata:                 #split line in first , second section                 barcode = line.split(":")[0]                 item = line.split(":")[1]                 price = line.split(":")[2]                 stock = line.split(":")[3]                  if barcode==usersitem:                     file = open("data.txt","r")                     #read each line of data vairble                     filedata = file.readlines()                     #close file                     file.close()                     print(item +"       £" + str(float(price)/100) + "      stock: " + str(stock))                     print("how many want buy")                     howmany= input()                     total+=float(price) * float( howmany)                     line in  filedata:                         #split line in first , second section                         barcode = line.split(":")[0]                         item = line.split(":")[1]                         price = line.split(":")[2]                         stock = line.split(":")[3]                         if barcode!=usersitem:                             open("data.txt","w")                             file.write(barcode + ":" + item + ":" + price + ":" +stock)                             file.close()                         else:                             stocklevel=int(stock)                             open("data.txt","w")                             file.write(barcode + ":" + item + ":" + price + ":" +str(int((stocklevel-float(howmany)))))                             file.close()                     open("data.txt","w")                     stocklevel=int(stock)                     print("do want buy item? [yes/no]")                     anotheritem = input()         if anotheritem == "n" or "n" or "no" or "no" or "no":                 print("total price: £"+ str(total/100)) 

there

if barcode!=usersitem:     open("data.txt","w") 

you need

if barcode!=usersitem:     file = open("data.txt","w") 

also same error have in else statement.

and should consider refactoring code, because have lot of file openings , closings.

edit: @roganjosh mentioned file builtin name in python 2, you'd better change occurrences of file e.g. f.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -