1

I am new to programming and was wondering if anyone can help me. I have created a program below that enables me to write to the text file. I have a third Column called flower_quantity. I was wondering how I can update the text file with the code below without overwriting the flower_quantity.

def feature_4(flower_file='flowers.txt'):

    flower_update = input("Enter the name of the flower you wish to change the price:"
                          "Lily, Rose, Tulip, Iris, Daisy, Orchid, Dahlia, Peony")
    flower_new_price = input("Enter the updated price of the flower")

    flower, price = [], []
    with open(flower_file) as amend_price:

        for line in amend_price:
            spt = line.strip().split(",")
            flower_price = int(spt[1])
            flower_name = str(spt[0])

            if flower_name == flower_update :
                price.append(flower_new_price)

            else:
                price.append(flower_price)

            flower.append(flower_name)

    with open(flower_file, "w") as f_:
        for i, v in enumerate(flower):
            f_.write("{},{}\n".format(v, str(price[i])))

    print("The new price of", flower_update, "is", flower_new_price)
Booboo
  • 38,656
  • 3
  • 37
  • 60
  • 1
    Does this answer your question? [How do you append to a file in Python?](https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file-in-python) – Booboo Apr 26 '20 at 11:31

3 Answers3

0

with open(path, 'a') will open your file in append mode, which will not delete contents and put the caret at end of file, so everything will be added at end of file.

You can find many reviews of all available file-opening modes, for example https://stackabuse.com/file-handling-in-python/

Alexander
  • 171
  • 7
  • Thank you for your response, however by using “a or a+” it adds a new flower to the file rather than editing the flower in it. I am trying to edit the price of the flower with the user input. The program that I created works, however, when I change the price it deletes the third column in the text file – Monica Mc Mullan Apr 26 '20 at 11:43
  • adding the input at the end of file == append. to change existing data you must move the caret to desired place. – Alexander Apr 26 '20 at 11:55
  • If you are using a file divided by columns I would suggest you use the csv format (you can use the `\t` as delimeter instead of comma, so when you open it in text editor it will have nice preview). There is a very good python module to work with csv files. – Alexander Apr 26 '20 at 11:57
0

Open the file in append mode

with open(flower_file,"a+"):

the + sign creates a new file if the file is not already present

this will append the file from its last written point. To append from a new line, you should start with \n

learner
  • 3,168
  • 3
  • 18
  • 35
QuantAC
  • 31
  • 1
  • 9
  • 1
    By using a+ it doesn’t let me append the data in the text file, it just adds the input at the end of the text file – Monica Mc Mullan Apr 26 '20 at 11:45
  • Just a clarification, append means adding something at the end of a file. I think you're getting append confused with overwriting – QuantAC Apr 28 '20 at 07:50
  • To better manage your entries, I suggest you look into CSV file format and python dictionaries. – QuantAC Apr 28 '20 at 07:54
0

There are a couple of ways to get this one done.

But following the way you have already been doing it, you could just include quantity when reading the file. The code would look a little like this.

def feature_4(flower_file='flowers.txt'):

    flower_update = input("Enter the name of the flower you wish to change the price:"
                          "Lily, Rose, Tulip, Iris, Daisy, Orchid, Dahlia, Peony")
    flower_new_price = input("Enter the updated price of the flower")

    flower, price, quantity = [], [], []
    with open(flower_file) as amend_price:

        for line in amend_price:
            spt = line.strip().split(",")
            flower_price = int(spt[1])
            flower_name = str(spt[0])
            quantity.append(str(spt[2]))

            if flower_name == flower_update :
                price.append(flower_new_price)

            else:
                price.append(flower_price)

            flower.append(flower_name)

    with open(flower_file, "w") as f_:
        for i, v in enumerate(flower):
            f_.write("{},{},{}\n".format(v, str(price[i]),quantity[i]))

    print("The new price of", flower_update, "is", flower_new_price)

Alternatively if you did want to update and not overwrite the entire file, you would need to open the file with open('txtfile.txt','a+'). and navigate to the specified line that you would like to append.

Michael
  • 110
  • 4