So I created a stock file system that uses txt files to store data. When the stock level goes below a certain value, I want to insert these products into a separate txt file. Whenever I do this, the file only has the last product's data, as each time the loop runs the next product's data overwrites the previous product's data.
EDIT: I should probably mention that I used a dictionary as the data soource, which is what each value such as SLref is assigned to
Here is the code:
with open("Under the Reorder Level.txt", "r+") as u:
for item in restockItem:
while count < len(restockItem):
u.write(gtin)
u.write(' ')
product = str(restockItem[count])
u.write(product)
u.write(' ')
SLref = restockItemStock[count]
stocklevel = str(restockItemStock[count]))
u.write(stocklevel)
u.write(' ')
RLref = restockItemReorder[count]
reorderlevel = str(restockItemReorder[count])
u.write(reorderlevel)
u.write(' ')
TLref = restockItemTarget[count]
targetlevel = str(restockItemTarget[count])
u.write(targetlevel)
u.write(' ')
u.write('£')
u.write(' ')
Pref = restockItemPrice[count]
price = str(restockItemPrice[count])
u.write(price)
u.write('\n')
count = count + 1
print (count)
u.close()
The output in the txt file reads:
"09876545 Tickets 6 10 50 £ 100"
When it should read:
"12345670 HairBrush 17 10 50 £ 1
10101018 PhotoFrame 11 10 50 £ 15
09876545 Tickets 6 10 50 £ 100"
EDIT NO.2: Every time the code is run, there are potentially going to be different stock levels, with either some new items under the stock level, or items that are no longer under the stock level. If these items are no longer under the stock level I don't want them to be written to this file. Therefore I would like the file to be cleared each time before writing to it. The items which are under the stock level are detected just before this block of code, and are then put into a list called "RestockItem" which is used above. I understand the different ways in which you can open a file, i'm just not sure on what I am now doing wrong. My previous error has been fixed however I am unable to get the correct output when i clear the file before using this loop.
open("Under the Reorder Level.txt", 'w+').close()
with open("Under the Reorder Level.txt", "a+") as u:
for item in restockItem:
while count < len(restockItem):
u.write(gtin)
u.write(' ')open("Under the Reorder Level.txt", 'w+').close()
with open("Under the Reorder Level.txt", "a+") as u:
for item in restockItem:
while count < len(restockItem):
u.write(gtin)
u.write(' ')
#carries on.......