0

I made a program that would make an account, store the name of the account to one file, and store all of the account's information to another file. To store all of the account names in a list on running the program I use this code:

account_names = []
def account_names_adder():
    try:
        with open('Account_Names') as namesfile:
            names = namesfile.readlines()
    except FileNotFoundError:
        None
    else:
        for name in names:
            if name.strip() == '':
                names.remove(name)
            else:
                account_names.append(name)

account_names_adder()

This is supposed to delete any whitespace and only account for strings with actual values to store in the list. However this is deleting the very first string with a value and not storing it in the list even though all other strings with values get appended. If I remove the .strip() it shows the first value, why is this? The program ran just fine one day ago and was never changed.

  • 1
    Add again the code, please. It's more difficult to help you otherwise. – A. Wolf Oct 19 '18 at 05:46
  • Do you want to clean the account names in the file Account_Names from extra spaces? I don't understand what is your goal. – A. Wolf Oct 19 '18 at 05:49
  • show file content sample? – HolaYang Oct 19 '18 at 05:52
  • use rstrip is you want to remove spaces only on the right – Gabriel M Oct 19 '18 at 05:57
  • @GabrielM and `lstrip` on the left :-) – U13-Forward Oct 19 '18 at 06:03
  • @A.Wolf sometimes blank lines appear in the file I store the account names in. If I don't strip them and remove them they will count as an account name, which I don't want. However when I strip them the first value with an actual account name is being removed and not appended while the following are account names are working fine. – bigredhawkeye Oct 19 '18 at 16:01
  • @TakeoffYoung [link] (https://docs.google.com/document/d/1-uk3jlgFojTpXEAI13f11o5b-78oBWTTvxhVgFhwQEc/edit?usp=sharing) This is an example of what the file might look like, the blank lines with spaces count as values so I strip the whitespace to remove them if they have no value. – bigredhawkeye Oct 19 '18 at 16:12

0 Answers0