1

so i am making a function that will save the resulted hash, and then i want to append all saved hashes in one file, how can i append the hashes in a new line each time? I tried the "\n" but it doesn't work, any ideas?

    save_hash = str(input("Do you want to save the hash value? < y - n >"))
try:
    if save_hash == "y":
        f = open("hash.txt", "a+")
        f.write(my_hash, "\n")
        print("Hash saved successfully, please check hash.txt file")
except:
    print("Saving failed, please try again ..")
Adam
  • 19
  • 4
  • Does this answer your question? [Correct way to write line to file?](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file) – kyriakosSt Jul 03 '20 at 14:57
  • 1
    @kyriakosSt Yes! I searched for it here but i didn't find it, looks like i missed it, thanks mate – Adam Jul 03 '20 at 14:58

1 Answers1

1

Your \n approach is correct. You just have a code error: write() accepts a simple string, so you need to append \n in the string representation of your hash value. Do it just like this:

    f.write("{}\n".format(my_hash))
kyriakosSt
  • 1,754
  • 2
  • 15
  • 33