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