Append list in Python -


i need first create empty list, insert 1 element , save disk. again read list disk , append element list , again save list disk , again read list , save element further operations , on. current code:

import pickle emptylist = [] # create empty list x = 'john' #this data coming client, change on each server call emptylist.append(x) # append element open('createlist.txt', 'wb') f: # write file       pickle.dump(emptylist, f) open('createlist.txt', 'rb') f: # read file       my_list = pickle.load(f) print my_list # print updated list 

now updated list this:

#if x = 'john' ['john'] #if x = 'george' ['george'] #if x = 'mary' ['mary'] # ..... , on 

what want append elements, this:

['john','george','mary',....]  

just change

emptylist = [] # create empty list 

to

emptylist = [] if not os.path.exists("createlist.txt") else pickle.load(open("createlist.txt")) 

that way if list exists load ... although no longer "emptylist"


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -