0

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

3
  • Next time, please include your code in your post, and not in an external link. I've edited it into your post this time Commented May 20, 2015 at 19:34
  • there are lots of different formats to write to files. Do you want to write the data to a file e.g. one name on each line of the file and then parse the input? Or do you actually want to save the data structure list and all its members? Commented May 20, 2015 at 19:37
  • @GC123UNC the file is purely for use of the program so its not needed for human use, so whatever the best way is for the program to retrieve the list for when the program is opened next Commented May 20, 2015 at 19:40

2 Answers 2

2

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'] 
Sign up to request clarification or add additional context in comments.

Comments

0

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() 

2 Comments

would you be able to break down whats happening in that for me, to help my understanding, Thanks:)
you should read this well written and simple tutorial about reading and writing files in Python: pythonforbeginners.com/files/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.