George Edmonds
George Edmonds

Reputation: 11

PYTHON - Writing a list to a file so it can be read and written to

I'm a complete rookie programmer and am making a basic 'membership' program to give me some experience with functions and lists So far i have this:

#Club membership program with functions

Club = ['George','Isaiah','Alby','Jack']

#FUNCTIONS=========================================================================================================================================================================================================

def remove_member():
    """Function to delete member from the club"""
    print('The club members are:', Club)
    removal_member = str(input('Enter the name of the member you would like to remove:'))
    if removal_member in Club:
        Club.remove(removal_member)
    else:
        print('Member not found')
    print('The club members are now:', Club)

def add_member():
    """Funtion to add a member to the club"""
    new_member = str(input('Enter the name of the member you would like to add:'))
    Club.append(new_member)
    print('The club members are now:', Club)

def functions():
    print('''
   List of current funtions:
   1 = Remove a member
   2 = Add a member
   3 = trherjuthyjht\n''')

#MAIN==================================================================================================================================================================================================================

print('HELLO AND WELCOME TO THE CLUB MEMBERS PROGRAM')
print('The current members of the club are:', Club)
functions()


run_program = input('Would you like to carry out a function? (y/n):')
while run_program == 'y':
    function_no = int(input('enter the the number of the funtion you would like to execute:'))
    if function_no == 1:
        remove_member()
        run_program = input('Would you like to carry out a function? (y/n):')
        functions()
    else:
        if function_no == 2:
            add_member()
            run_program = input('Would you like to carry out a function? (y/n):')
            functions()

input('Thanks you for using the Club membership program, press enter to exit.')

what i want to do next is expand my file handling skills: i want to be able to write the list to a file so everytime i open or close the program the list can be retrieved

  1. how can i do this
  2. what file type will i need to save it as for python to recognise it as a list

any further help will be much appreciated

Upvotes: 0

Views: 71

Answers (2)

Gil
Gil

Reputation: 380

What you are describing is data serialization. There are many formats.

For example, have a look at the pickle module. Example usage:

 >>> import pickle
 >>> l = ["my","list","of","strings"]
 >>> pickle.dump(l,open('pickledfile.txt','wb'))    
 >>> pickle.load(open('pickledfile.txt','rb'))
    ['my', 'list', 'of', 'strings']

Upvotes: 2

WilliamSF
WilliamSF

Reputation: 284

Here are two functions to save and load your list:

Club = ['George','Isaiah','Alby','Jack']
def saveClub(club):
    myfile= open('myfile.txt','w')
    for member in club:
        myfile.write(member+'\n')

def loadClub():
    newClub=[]
    with open('myfile.txt', 'r') as ins:
        for line in ins:
            newClub.append(line.rstrip('\n'))
    return newClub

saveClub(Club)
Club=loadClub()

Upvotes: 0

Related Questions