在Python中读取,写入和附加到文本文件。 IOError:[Errno 0]错误

时间:2016-06-14 00:14:59

标签: python append bank

我正在尝试为一个学校项目制作一个小型银行软件程序,并且在一段时间内一直在努力解决这个问题。我正在使用python。

现在,该程序应该做的是能够将名称添加到名为accounts的文本文件中,然后通过键入帐户的确切名称来访问这些银行帐户,这样可以正常工作。问题是我只能在文件为空并且通过程序的同一运行时添加帐户。

当我尝试停止程序然后重新启动它并向该文件添加更多帐户时,它会询问名称和帐户类型,但它会在第16行显示错误,

  

f.write(accName +" \ n")

它显示的错误是

  

IOError:[Errno 0]错误

如何在不删除此行的情况下使用户能够在初始运行和创建文本文件后添加帐户?

  

searchFile = f.read()。splitlines()

这是我的整个计划。

again = "y"
f = open('accounts.txt', 'a+') #Opens the file in a+ mode
searchFile = f.read().splitlines() #Need this to be able to look through the file and write back a certain line, and the next two lines
accessedAccount = []

choice = raw_input("What would you like to do? (add/remove a bank account, access a bank account): ")

if choice == "a":
    while again == "y":
        accName = raw_input("Account owner's name: ")
        accType = raw_input("Account type: ")
        accBal = "0"

        f.write(accName + "\n")
        f.write(accType + "\n")
        f.write(accBal)
        f.write("\n")

        again = raw_input("Add another account?: ")

if choice == "a2":
    account = raw_input("What is the name of the account you wish to access?: ")
    for i, line in enumerate(f):
        if account in line:
            for j in f[i:i+3]:
                print j
                accessedAccount.append(j)

    balance = accessedAccount[2]
    intBalance = int(balance)
    print accessedAccount

    choice2 = raw_input("This is your bank account. What would you like to do now? (Withdraw/deposit, exit): ")
    if choice2 == "d":
        amount = int(raw_input("How much money would you like to deposit? Current balance: %i : " %intBalance))
        newBalance = intBalance + amount
        print "Current balance: %i" %newBalance
        for i, line in enumerate(searchFile):
            if balance in line:
                newBalance = str(newBalance)
                line.replace(balance, newBalance)
                print "test"

f.close

0 个答案:

没有答案