2

I have code like this:

def export_devices():
        code = input("Enter device code: ")
        amount = int(input("How many devices you export: "))
        with open("uredjaji.txt", "r+") as f:
            current_position = 0
            line = f.readline()
            while line:
                if line[:len(code) + 1] == code + ":":
                    line = line.rstrip()
                    amount_index = line.rfind(":") + 1
                    current_amount = int(line[amount_index:])           
                    if amount > current_amount:
                        print("There no that many devices in stock...")
                        return
                    remaining_content = f.read()
                    f.seek(current_position)
                    f.truncate()
                    line = line[:amount_index] + str(current_amount - amount) + "\n"
                    f.write(line)
                    f.write(remaining_content)
                    return
                current_position = f.tell()
                line = f.readline()
                with open('transakcije.txt','a') as transactions:
                    date = datetime.date.today().strftime('%d.%m.%Y.')
                    transactions.write("1" + ":" + str(amount) + ":" + "export" + ":" + str(date) + ":" + "username" + "\n")
        print("Error device code: {}".format(code))

Now I would like to my "transakcije.txt" looks like this:

1:3:iznos:17.06.2017.:username

But it always append the same line for three times. With any other kind of indentation it won't append at all.

Also, my uredjaji.txt file looks like this:

tw004:Galaxy S5:Samsung:Mobilni telefon:3
tw002:Galaxy S6:Samsung:Mobilni telefon:1
tw001:Huawei P8:Huawei:Mobilni telefon:1
tw003:Huawei P9:Huawei:Mobilni telefon:100998

P.S: "username" should be variable from another function, so if someone could help me how to write that variable in this file I will be so thankfull. :)

Нео
  • 63
  • 2
  • 10
  • Can you explain what you are trying to do? Your code is hard to follow. – Joshua Jun 17 '17 at 04:22
  • Can you post your current output text file? This would help us verify the problem. – Adeel Ahmad Jun 17 '17 at 04:23
  • My transakcije.txt file now looks like this: `1:3:iznos:17.06.2017.:username 1:3:iznos:17.06.2017.:username 1:3:iznos:17.06.2017.:username` And I'm trying to write only one line for ever using this function. – Нео Jun 17 '17 at 04:26
  • Have you tried de-indenting with `open('transakcije.txt','a') as transactions:` part one level? – Adeel Ahmad Jun 17 '17 at 04:33
  • The contents of `uredjaji.txt` should be posted as well. Is it three lines long? Does it fail `if line[:len(code) + 1] == code + ":":` each time? – Joshua Jun 17 '17 at 04:34
  • Just added uredjaji.txt file... :) – Нео Jun 17 '17 at 04:36

1 Answers1

0

When you open the file, you read a single line and then go into a while loop on the existence of line. If you do not get a match on the input code, you then attempt to, I suppose, reposition the file pointer with f.tell() but you do not do a seek. Thereafter, you read the file again and write transakcije.txt. Sadly, the original while loop is still in play, so you will write transakcije.txt multiple times.
It is not clear what you are attempting to achieve with this code but you need to sit down and rethink it from the ground up.
If it is some sort of stock reporting/replenishment routine, I can't help thinking that a database (sqlite3 as a simple starter) would be more appropriate that pulling ascii files apart.

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60