I keep getting this error and I dont know hove to fix it
<p>I open the file but is saying the file is not open. I stuck on what to do . I am new to python.</p>

<p>Here is the error:</p>

<pre><code>Traceback (most recent call last):
File "\\users2\121721$\Computer Science\Progamming\task3.py", line 43, in   &lt;module&gt;
file.write(barcode + ":" + item + ":" + price + ":" +str(int((StockLevel-    float(HowMany)))))
    ValueError: I/O operation on closed file.
</code></pre>

<p>Here is the code:</p>

<pre><code>#open a file in read mode
file = open("data.txt","r")
#read each line of data to a vairble
FileData = file.readlines()
#close the file
file.close()

total = 0 #create the vairble total
AnotherItem = "y" # create
while AnotherItem == "y" or AnotherItem  == "Y" or  AnotherItem ==  "yes" or  AnotherItem ==  "Yes" or  AnotherItem ==  "YES":
        print("please enter the barcode")
        UsersItem=input()
        for line in  FileData:
                #split the line in to first and 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 to a vairble
                    FileData = file.readlines()
                    #close the file
                    file.close()
                    print(item +"       Â£" + str(float(price)/100) + "      Stock: " + str(stock))
                    print("how many do you want to buy")
                    HowMany= input()
                    total+=float(price) * float( HowMany)
                    for line in  FileData:
                        #split the line in to first and 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 you want to buy another item? [Yes/No]")
                    AnotherItem = input()
        if AnotherItem == "n" or "N" or "no" or "No" or "NO":
                print("total price: Â£"+ str(total/100))
</code></pre>



<p>There is </p>

<pre><code>if barcode!=UsersItem:
    open("data.txt","w")
</code></pre>

<p>You need to </p>

<pre><code>if barcode!=UsersItem:
    file = open("data.txt","w")
</code></pre>

<p>Also the same error you have in <code>else</code> statement.</p>

<p>And you should also consider refactoring your code, because you have a lot of file openings and closings.</p>

<p><em>Edit</em>: as @roganjosh mentioned <code>file</code> is a builtin name in Python 2, so you'd better change all occurrences of <code>file</code> to e.g. <code>f</code>.</p>

python