Search code examples
pythontext-files

Removing user input from a Text file


I am trying to remove specific student ID numbers from a text file. the first if can verify if an ID is in the file. the second can add to the file but the third I just can't get any solutions to work for me.

def change_memberships():
    membership = ''
    verify = ''
    filename = 'Member.txt.txt'
    new_a_num = ''
    remove_mem = ''
    id_ = ''
    
    print('1) Verify an existing account?')
    print('2) Add a new member?')
    print('3) Remove a member?')
    membership = input('Which would you like to do: ')

    if membership == '1':
        verify = input('Please enter a Middlesex ID number: ')
                
                
        #Open file and check if the A number is in the file or not
        with open(filename) as f_obj:
            Anumber = f_obj.read()

            name_to_check = verify
            #If A number is in file return discount
            if name_to_check in Anumber:
                print('This ID has a preexisting account.')
                #If A number is not in file return 'A number not in file'
            else:
                print('This Student ID number is not in our records.')
    elif membership == '2':
        file = open('Member.txt.txt', "a")
        new_a_num = input('Please enter the MCC Student ID number you wish to add: ')
        file.write('\n' + new_a_num)
        print('MCC Student ID has been added.')
        file.close()
    elif membership == '3':
        remove_mem = input('Please enter the MCC Student ID number you wish to remove: ')




change_memberships()

I have tried importing my files and variables in about 6 or 7 different solutions to the exact same problem I am having from this website but they all either deleted all the IDs in my file, did nothing at all or just added random spaces around the ID I'm trying to remove, indentations and large spaces between the IDs in the file. I have no idea where to even begin now because each solution I have tried has different code, so I don't know what's right or wrong anymore.


Solution

  • You could first read the files and get the list of current Ids, then open the file with 'w' (create a new blank file), and check if each Id in list of current Ids equal to removed Id. If it doesn't then write that Id in list of current Ids to the new blank file, else (Id = removed Id) then skip that Id (doesn't write to new file).

    def change_memberships():
        membership = ''
        verify = ''
        filename = 'sof.txt'
        new_a_num = ''
        remove_mem = ''
        id_ = ''
        print('1) Verify an existing account?')
        print('2) Add a new member?')
        print('3) Remove a member?')
        membership = input('Which would you like to do: ')
    
        if membership == '1':
            verify = input('Please enter a Middlesex ID number: ')
    
            # Open file and check if the ID is in the file or not
            with open(filename) as f_obj:
                Anumber = f_obj.read()
    
                name_to_check = verify
                # If ID is in file return discount
                if name_to_check in Anumber:
                    print('This ID has a preexisting account.')
                # If ID is not in file return 'ID not in file'
                else:
                    print('This Student ID number is not in our records.')
        elif membership == '2':
            file = open('sof.txt', "a")
            new_a_num = input('Please enter the MCC Student ID number you wish to add: ')
            file.write('\n' + new_a_num)
            print('MCC Student ID has been added.')
            file.close()
        elif membership == '3':
            remove_mem = input('Please enter the MCC Student ID number you wish to remove: ')
            # Open the file and read the contents into a list
            with open(filename, 'r') as f:
                lines = f.readlines()
            # Iterate through the list, write Id to new file if it isn't equal removed_mem
            with open(filename, 'w') as f:
                removed = False
                for line in lines:
                    if line.strip() == remove_mem:
                        removed = True
                        print('ID number removed.')
                    else:
                        f.write(line)
                if not removed:
                    print('ID number not found in file.')
    
    
    change_memberships()