2017-01-26 18 views
0

は、ここに私の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)) 

最後の二つの下であれば、新たな量のファイル(m.write)私は上書きしたい一方で、それは、ファイルに追加に書かれている文を、追加しません。これどうやってするの?私は "r +"としてファイルを開いています。

+0

関連:[R '+' と 'A +' ときPythonで開いているファイルの違いは何ですか?](http://stackoverflow.com/questions/13248020/whats-the-difference-between-r-オープン時にファイルを開くときのpython)と[python open組み込み関数:モードa、a +、w、w +、r +?の違い](http://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-modes-aaww-and-r) – blacksite

+0

ファイル内のすべての行に預金または引き出しを追加/減額する理由は何ですか?それが単なる数字の場合、なぜそれを読むために 'for'ループを使用していますか? – Barmar

+0

ようこそStackoverflowへ!サイトを最大限に活用するには、[最小限の、完全で、検証可能な](http://www.linux.org/)の作成を含む[良い質問をする](http://stackoverflow.com/help/how-to-ask) //stackoverflow.com/help/mcve)の例です。 –

答えて

0

あなたは

for info in m: 

を使用するとPythonはファイルを読み込みます。ファイルの位置は、情報を読み取った場所の後ろに残り、m.write()を使用すると、その位置に書き込みます。ファイルの最後まで読み込んだ場合は、m.write()がファイルに追加されます。

実際には、Python仕様ではファイルオブジェクトと一緒に動作する方法が特定されていないため、ループ中に同じファイルに書き込むべきではありません。それはファイルの最後に書き込むかもしれない、それは真ん中に書くかもしれない。しかしどちらの方法でも、最初に戻ってファイルを上書きしません。

ファイルから残高を読み、ファイルを閉じたり、追加または減算して新しい残高を取得したりして、ファイルを再度上書きして上書きします。

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)) 
関連する問題