0

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.......
AntsOfTheSky
  • 195
  • 2
  • 3
  • 17
  • My guess is that the data is not being written to the file. If you use `with open` you do not have to close the file by calling `close()`. It is hard to predict when the file is being open and closed, most likely the root of the problem is there – taras Jan 12 '17 at 20:00
  • Ohh okay thanks for that. And I can see it is being written, but for some reason only one line is being written when i use that new line of code... or maybe each line is being overwritten again? – AntsOfTheSky Jan 12 '17 at 20:01
  • 1
    I'm so sorry that i wasted your time but i have figured it out! I put that new line in the wrong place or something because i moved it and it worked.. Thank you for your help, really appreciate it! – AntsOfTheSky Jan 12 '17 at 20:03
  • that is what could happen. Try to use a database like sqlite. It will make your life easier :) If you come up with a sample of code that runs and reproduces the issue - we will find the problem, otherwise it is just guessing. – taras Jan 12 '17 at 20:03
  • No problem if you promise me to learn how to work with databases :D – taras Jan 12 '17 at 20:04
  • I saw that sqlite was a possibility, looked it up and was like "NOPE" because it looked super complicated.... – AntsOfTheSky Jan 12 '17 at 20:15

1 Answers1

1

Change the flag to open the file from r+ to a+. which means for writing and reading from the end of the file.

Here is a difference between them explained on SO

If you want to completely overwrite the file, then you can use 'w' option, for writing. Here is a list of flags explained

Community
  • 1
  • 1
taras
  • 3,579
  • 3
  • 26
  • 27
  • I would like this file to be rewritten every time the program is run though, so if the stock data changed this file will be up to date, will that work or will it always just have data added to it only? – AntsOfTheSky Jan 12 '17 at 19:35
  • I see that it only appends to what was there previously. Is there a way/line of code that will clear the contents of a file that i could put before this bit of code? – AntsOfTheSky Jan 12 '17 at 19:36
  • I used a line "open("Under the Reorder Level.txt", 'w+').close()" before the chunk of code seen above, and the output only shows one of the desired products like last time, but when i don't use that newest line of code the method works fine...? – AntsOfTheSky Jan 12 '17 at 19:45
  • Could you please provide more information on what you do. The key here is to understand how file 'modes' work. Sometimes you want to read file and use 'r', sometimes only write 'w', and sometimes you want to read file 'r+' and write to it in the ned. – taras Jan 12 '17 at 19:49
  • I've added more detail at the bottom. Thanks for your help. – AntsOfTheSky Jan 12 '17 at 19:54