1

So I am trying to make a piggybank in Python that uses file IO by opening a file that I already made and replacing the current text with new text. Then it will save the file and that is how it saves the money. But, it is not working and tells me that there is an error. Can somebody help?

def piggybank():
    file = open('piggybank.txt','wb')
    addedmoney = input('How much money are you adding?')
    previousamount = file.read()
    newamount = addedmoney + previousamount
    for line in file:
        line.replace(newamount)
    print("You now have:\n", newamount)
PokeBros
  • 51
  • 1
  • 2
  • 7
  • Can you post the error you are getting? – Kevin K. Feb 06 '17 at 17:26
  • Traceback (most recent call last): File "", line 1, in File "C:\Users\isaac_ofpqxf1\Desktop\W3 Exercises.py", line 241, in piggybank previousamount = file.read() io.UnsupportedOperation: read – PokeBros Feb 06 '17 at 17:29
  • You are opening the file in write mode. Try opening the file in read mode (`open('piggybank.txt','r')`) to get the data, then close it and reopen it in write mode before writing it. – Kevin K. Feb 06 '17 at 17:34
  • is there a read and write mode? – PokeBros Feb 06 '17 at 17:35
  • http://stackoverflow.com/questions/13265466/read-write-mode-python – Kevin K. Feb 06 '17 at 17:40

0 Answers0