1

I'm trying to make a little bank software program for a school project and have really been struggling with this for a little while. I'm using python.

Now, what this program is supposed to do is be able to add names to a text file called accounts and then be able to access those bank accounts by typing in the exact name of the account, which works fine. The problem is I can only add accounts when the file is empty and through the same run of the program.

When I try to stop the program and then restart it and add more accounts to the file, it asks for a name and account type, as it should, but then crashes showing an error on line 16,

f.write(accName + "\n")

The error it shows is

IOError: [Errno 0] Error

How can I make it so the user is able to add accounts after the initial run and creation of the text file without removing this line?

searchFile = f.read().splitlines()

Here's my entire program.

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
paisanco
  • 4,098
  • 6
  • 27
  • 33
  • I, for one, will need more information. I had no trouble adding information in multiple runs, even trying to access it later (it doesn't work yet for lack of balance info, but that's a later problem). The only problem I see is that you're missing the parentheses on **f.close()**, but this didn't hurt the run under my environment, since all open files are properly cleaned up upon program exit. – Prune Jun 14 '16 at 00:22
  • @Prune Thank you for the response. I just tried adding the parentheses on f.close and it didn't work, oops. As for more information, Basically what happens is when I run the program after deleting the accounts file so it has to recreate it, the program runs fine and I can add all the accounts I want as long as I use my restart flag, but when I hit stop or end the program then try to run it again and add more accounts, it gives me IOError: [Errno 0] Error. Hope that helped at all, not sure exactly why this is happening – Ryan Gugelmeier Jun 14 '16 at 00:31
  • Please supply some tracking information. For instance, I put **print searchFile** right after the **splitlines** use, and I got the expected output from my previous input: ['Able', 'elite', '0', 'Baker', 'pica', '0', 'Charlie', 'saving', '0', 'Delta', 'dawn', '0'] – Prune Jun 14 '16 at 00:34
  • Check http://stackoverflow.com/questions/11176724/python-file-operations, adding `f.seek(os.SEEK_END)` just after `read` should fix the issue in case you're using Windows. – niemmi Jun 14 '16 at 00:36
  • @Prune Sorry I'm not sure what tracking information is as I'm still pretty new to all of this. However I added print searchFile as well to my code in the same spot and it does give the desired outcome as well, however when I try to add more accounts to the text file it gives me the IOError after entering a name and account type – Ryan Gugelmeier Jun 14 '16 at 00:42
  • What OS are you using? It runs just fine on my Ubuntu 14.04 machine, using python 2.7. My one suggestion would be to use the `with open('filename') as f:` construct, rather than opening and closing the file manually. This will deal with cleanup even in the case of an exception. – Andrew Guy Jun 14 '16 at 05:31

0 Answers0