1

Here is my python code:-

f= open("Passes.py", "a+")
m=open("money.py","r+")
passes= {}
init={}
initial=0
import time
print "Welcome to the virtual banking system"
time.sleep(0.5)
user=raw_input("Would you like to create a new account? 'Y/N'").lower()
if user== "y":
  new_user= raw_input("Create a username:")
  new_pass= raw_input("Create a password:")
  p= passes[new_user]= new_user + ":" + new_pass
  f.write(str(p+ "\n"))
  ask=raw_input("Would you like to sign into your account? 'Y/N'").lower()
  if ask=="y":
    user_in=raw_input("Enter your username:")
    if user_in==new_user:
      pass_in=raw_input("Enter your password:")
      if pass_in==new_pass:
        print "Welcome to your account" + " " + new_user
        useropt=raw_input("Would you like to view your balance- enter 1, deposit money- enter 2, withdraw money- enter 3 or exit- enter 4:")
        if useropt=="1":
          print "Your balance is:", initial
        if useropt=="2":
          amountdep= int(raw_input("How much money would you like to deposit?:"))
          initial+=amountdep
          print "Thanks. Your new balance is:", initial
        if useropt=="3":
          amountwith=int(raw_input("How much would you like to withdraw?:"))
          initial-=amountwith
          print "Your balance is:", initial
        i=init[new_user]=str(initial)
        m.write(str(i)+"\n")
    else:
      print "Password not valid"

  else:
    print "Username does not exist"
else:
  user2=raw_input("Do you have an existing account? 'Y/N'").lower()
  if user2=="y":
    existing_user=raw_input("Enter your username:")
    exisitng_pass=raw_input("Enter your password:")
    for passwords in f:
      if passwords==existing_user+":"+exisitng_pass:
        print "Welcome to your account" + " " + existing_user
        useropt2=raw_input("Would you like to view your balance- enter 1, deposit money- enter 2, withdraw money- enter 3 or exit- enter 4:")
        if useropt2=="1":
          for info in m:
            print "Your balance is:", info
        if useropt2=="2":
          amountdep= int(raw_input("How much money would you like to deposit?:"))
          for info in m:
            a=int(info)+int(amountdep)
            print "Your new balance is:", int(a)
            m.write(str(a))
        if useropt2=="3":
          amountwith=int(raw_input("How much would you like to withdraw?:"))
          for info in m:
            t=int(info)-int(amountwith)
            print "Your balance is:", t
            m.write(str(t))

Under the last two if statements, when the new amount is written to the file(m.write), it appends to the file, whereas I would like to overwrite it not append it. How can I do this? I have opened the file as "r+".

Sam
  • 289
  • 3
  • 11
  • Related: [What's the difference between 'r+' and 'a+' when open file in Python?](http://stackoverflow.com/questions/13248020/whats-the-difference-between-r-and-a-when-open-file-in-python) and [python open built-in function: difference between modes a, a+, w, w+, and r+?](http://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r) – blacksite Jan 26 '17 at 00:36
  • Why are you adding/subtracting the deposit or withdrawal to every line in the file? If it's just one number, why are you using a `for` loop to read it? – Barmar Jan 26 '17 at 00:48
  • Welcome to Stackoverflow! To get the most out of the site it is important to [ask good questions](http://stackoverflow.com/help/how-to-ask), that includes creating a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example. – Stephen Rauch Jan 26 '17 at 02:34

1 Answers1

1

When you use

for info in m:

Python reads the file. The file position is left after the place where it read the info, and when you use m.write(), it writes to that position. If it read to the end of the file, m.write() will append to the file.

Actually, the Python specification is not specific about how for works with file objects, so you shouldn't write to the same file during the loop. It might write to the end of the file, it might write in the middle. But either way, it's not going to go back to the beginning and overwrite the file.

What you should do is read the balance from the file, close the file, add or subtract to get the new balance, then open the file again to overwrite it.

with open("money.py", "r") as m:
    info = int(m.readline().strip())
useropt2=raw_input("Would you like to view your balance- enter 1, deposit money- enter 2, withdraw money- enter 3 or exit- enter 4:")
if useropt2=="1":
    print "Your balance is:", info
if useropt2=="2":
    amountdep= int(raw_input("How much money would you like to deposit?:"))
    a=info+int(amountdep)
    print "Your new balance is:", a
    with open("money.py", "w") as m:
        m.write(str(a))
if useropt2=="3":
    amountwith=int(raw_input("How much would you like to withdraw?:"))
    t=info-int(amountwith)
    print "Your balance is:", t
    with open("money.py", "w") as m:
        m.write(str(t))
Barmar
  • 741,623
  • 53
  • 500
  • 612