'Program saving duplicates to config, but only after the second run

I am writing a small program (password-manager[just for fun, not for usage]) and I am saving my "accounts" with the configparser. Now i want to make it into a Qt and a CLI program but whenever i write a password with configWriter() the next time i do it again, the config contains duplicate accounts and crashes when displaying them as there is a duplicate. I can't find my mistake and its very weird. So when i add a account with the name "StackOverflow" and i display it it works, but the next time I add a account with the name "Discord" it generates "Discord" and another "StackOverflow" account in the config. It doesn't make sense? (Removed unnecessary parts, as they all just rely on the configWriter() and listPassword() functions)

This is the error btw:

configparser.DuplicateSectionError: While reading from 'test.ini' [line  5]: section '<the name of the section>' already exists

Edit: I seem to have found a solution: Changing the access of open to write instead of append seems to work, but i don't understand why so it isn't a proper "solution" for me.

    config = configparser.ConfigParser()

    def configParser(file='test.ini'):
        config.read(file)
        return config

    def configSections(file='test.ini'):
        config.read(file)
        return config.sections()
    
    def configWriter(name, mail, password, totp=None, file='test.ini'):
        if name in configSections():
            pass
        else:
            if totp is not None:
                config[name] = {'pass': password, 'mail': mail, 'totp': totp}
            else:
                config[name] = {'pass': password, 'mail': mail}
            with open(file, 'a') as f:
                config.write(f)
    
    def configEraser(section, file='test.ini'):
        with open(file, 'w') as f:
            config.write(f)
        config.remove_section(section)
    
    def totpParser(key):
        totp = pyotp.TOTP(key)
        return totp.now()
    
    def passwordGen(length=24):
        password = []
        strings = string.ascii_letters + string.digits + "#!+-"
        for i in range(length):
            password.append(random.choice(strings))
        return "".join(password)
    
    def listPasswords():
        passwords = []
        for i in range(len(configSections())):
            read = configParser()[configSections()[i]]
            args = configSections()[i] + ': ' + read['mail'] + ':' + read['pass']
            if 'totp' in config[configSections()[i]]:
                try:
                    passwords.append(args + ' | ' + totpParser(read['totp']))
                except:
                    passwords.append(args)
            else:
                passwords.append(args)
        return passwords


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source