This program let you edit,save and load a list. This program also demonstrate how to use files, pickle (or cPickle), and built-in list method.
Python, 93 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#!/usr/bin/python # Filename : lister.py import sys, pickle global mylist mylist =  def takeinput(): # take input from user to select which function try: num = int(raw_input('--> ')) if num == 1: printlist() elif num == 2: add() elif num == 3: delete() elif num == 4: save() elif num == 5: load() elif num == 6: quit() else: quit() except ValueError: quit() def printlist(): # print mylist print 'mylist is now:', mylist, '\n' def add(): # add an item to mylist s = str(raw_input('Enter the name of the object you want to add --> ')) mylist.append(s) # add the del s printlist() def delete(): # delete an item from mylist print # newline i = 0 while i < len(mylist): # use while loop to assign 'itm' to the for itm in mylist: # items in the list. Then, print the number of print itm,'is',i # items and items in mylist. i = i + 1 # the numbers of the items and items is printed. print # newline s = int(raw_input('Enter the number of the object you want to delete --> ')) del mylist[s] # delete the item using both 'del' function & indexing method del s printlist() def save(): # save mylist mylist_data = 'lister.data' # The name of the file f = file(mylist_data, 'w') # Open for 'w'riting pickle.dump(mylist, f) # dump/put the list into the file f.close() # close print 'Saved mylist :)' def load(): # load mylist mylist_data = 'lister.data' f = file(mylist_data) storedlist = pickle.load(f) print 'Loaded mylist :)' def quit(): # quit the program print 'Bye! :) \n' sys.exit() # Script starts here print '''\ Hi! this is a lister program. Type: 1- print mylist 2- add an object to mylist 3- delete an object from mylist 4- save mylist 5- load mylist 6- quit\ ''' running = True while running: takeinput() # Always take input from user until he/she quits the program
I write this program just to practice my Python skills to use list methods, files and pickle. It can be useful for someone whose want to learn using pickle and store list in your hard disk. Lastly, use it for fun :)
This "lister module" I tried this module. It is neat. But, it will save a file (that contains strings for data), O.K. But when I try to reload the (saved) file I get an empty string back. If I look in the directory, such as c:\Python24 (Windows) I can find the saved file and open it with Notepad, and it is a fresh file with a date/time group that is new. However, this code will not reload the file, when attempting a reload, it says the string (file) is empty ????
The Load function works!! The load function does work! The thing is the list isnt pased back to the rest of the program, so it disapears when the function returns. If the load function would end with something like:
and be caled (in the event handling loop) with something as
global_list_name = Load()
then the list will be pased back to the main program
alternatively inside the Load function you can explicitly assign to the global variable(something like global.listname = ... but Im not sure)
sorry please remove. due to first needing to subscribe, and the break in the train of thought that causes I have inadvertedly put this comment in the wrong thread.