-1

I'm creating a very basic banking record program in python 3.6. I created a file "accounts.txt" in which users can add their accounts -- name, account number, balance. After they add, the file looks something like this:

213123 |dgfdg |21312.0
123124 |vxcvx |213123.0
123123 |sfsdf |123123.0

column represents account no., name, balance respectively.

I want to delete a particular record from this file. I take account no. as input of the record to be deleted. I just can't figure how to do it. Ex- if the user enters 213123, the entire record should be deleted.

A separate problem in the same case- when I ask the user to input their account number, name, balance, i want to scan "accounts.txt" and if the account number already exists, produce an error instead of creating a new record. Avoid duplicate account numbers.

I'm new to python and this is a basic program so please don't suggest using mysql or mariadb. :D

def account_set():
    accno = int(input("Enter Account Number \t"))
    name = str(input("Enter Name \t"))
    balance = float(input("Enter Balance \t"))
    return accno, name, balance

def new():
    account = open("accounts.txt", "a+")
    info = account_set()
    account.seek(0, 2)
    for items in info:
        account.write("{0:<25} {1}".format(items, "|"))
    account.write("\n")
    account.close()

def delete():        
    accno = (input("Enter Account number to delete"))
    file = open("accounts.txt", "r+")


print("""
Please choose any one of the Following Options: \n \n
1. New Account \n
2. Delete Account \n
3. Exit
""")

x = True
while x:
    user_input = int(input("Enter an option \t"))
    if user_input == 1:
        new()
    elif user_input == 2:
        delete()
    elif user_input == 3:          
        print("Thank you for banking with us.")
        x = False
    else:
        print("Invalid Choice")
K. Grewal
  • 51
  • 1
  • 12
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. StackOverflow is not a design, coding, research, or tutorial service. "I just can't figure out how to do it" suggests that you need an hour with a local tutor in how to analyse a problem statement; Stack Overflow is for specific programming problems. – Prune Mar 30 '17 at 17:07

1 Answers1

0

A possible way to do this is to ask the user for input and see what number it is. If the number is 213123, then you can use this code:

filename.seek(0)
filename.truncate()

The code above will delete the entire file.

This SO post explains how to delete single lines perfectly: Deleting a specific line in a file (python)

To see if an account number exists, you can read the file like this:

 f = open("filename.txt").readlines()
 f = [i.strip('\n') for i in f]
 file = [map(int, i.split()) for i in f]
 #then, you can loop through file and see if the account number exists:
 #account numbers are first in the list, so you can make a list of them
 account_numbers = [i[0] for i in file]
 number = 123456
 if number in account_numbers:
     print "account number ", number, " all ready exists"

 else:
     print "account number", number, " does not exist"
Community
  • 1
  • 1
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • i can only use this: filename.seek(0) filename.truncate() if i know in which position the record is. I need a general algorithm because everytime i want to delete a record i'm not going to open the file and see where the record is and then change my code to delete that particular record. Hope you understand. – K. Grewal Mar 30 '17 at 17:50
  • Also that link you provided, doesnt solve my question. Because lets say if some record has account number 123 and some other record has balance of 123. Both records will be deleted if i simple check the each line for '123'. – K. Grewal Mar 30 '17 at 17:56