我的python代码是追加而不是覆盖。我该怎么覆盖?

时间:2017-01-26 00:34:19

标签: python

这是我的python代码: -

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))

在最后两个if语句中,当新的金额写入文件(m.write)时,它会附加到文件中,而我想覆盖它而不是附加它。我怎样才能做到这一点?我打开了文件" r +"。

1 个答案:

答案 0 :(得分:0)

使用时

for info in m:

Python读取文件。文件位置保留在读取信息的位置之后,当您使用m.write()时,它会写入该位置。如果它读到文件的末尾,m.write()将附加到文件。

实际上,Python规范并没有具体说明for如何处理文件对象,因此在循环期间不应该写入同一个文件。它可能写到文件的末尾,它可能写在中间。但不管怎样,它都不会回到开头并覆盖文件。

您应该做的是从文件中读取余额,关闭文件,添加或减去以获得新余额,然后再次打开文件以覆盖它。

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))