I am trying to write a very simple script that would simulate a pocket secretary for my homework, but I have a small issue. I want all my data to be saved in a file (abstract form) each time I exit the program and then when i boot it up I want to read that file, eval(stack) (basically load the data into memory) then verify if it s valid ... anything that isn t is deleted. This is what I ve done:
def loadToString (stack):
f = open ("data.txt", "w")
load_stack = str(stack) #convert to string
f.write (load_stack) #write in file
f.close()
def loadFromString ():
f = open ("data.txt", "r")
load_stack = f.readline() #read string
if (load_stack == "" or load_stack == " " or load_stack == None):
stack = []
return stack
else:
stack = eval(load_stack) #trying to convert from string to list
f.close ()
我的问题是,在我看一看档案时,它不是真的说谎,因为它缺乏trop子;“和”;因此,当我试图“老(打)”一些错误,我的“历史”最终变成空洞时(当我试图用空洞的 st——None-时)
我无法理解为什么发生这种情况。 另外,I m ok有快速固定装置(在txt档案中人工添加外线)
I have no idea how to check if a file is empty or if I read an empty line so I d also appreciate if you could help me with the "if" statement in "loadFromString". About eval () ... like I said this is just homework so I m not afraid about any malicious intents.