0

我有一个绑定到键的函数此函数将某些内容写入文件,因为它应该:

def saveGame():
from modules.logic import game
from modules.logic.player import Player
from modules.logic.light import Light
filename = str(time.time())
data = open("myData", "a")
lis1 = []
if (len(game.active_entities)>0):
        for key in game.active_entities:
            if (not isinstance(game.active_entities, Light)):
                if(not isinstance ( game.active_entities, Light)):
                        ent = game.active_entities[key]
                        ent2 = ent.networknew()
                        lis1.append(ent2) 
        lis1.insert(0, game.get_player().networknew())
        lis1.insert(0,game.seed)
        saved = open(filename, "w")          
        pickle.dump( lis1,saved, 2)
        try:
            data.write(filename+"\n")
        except Exception as e:
            print e

按下某个键时会调用 savegame。它创建一个新文件,在其中保存一些内容(保存的内容在这里并不重要),然后将新文件的名称保存在另一个文件中。

但是,当我结束并重新启动程序时,第二个文件是空的,即使如此,只要程序仍在运行,它的内容就正确。我想在程序关闭后仍然拥有内容。那可能吗?如何?

对于你的问题:

我通过打开一个窗口开始我的游戏:

class StartWindow:

def __init__(self):


    data = open("myData", "w")
    data.close()
    data = open ("myData", "rb")
    self.master = Tk()       
    if (not data.readline()== ""):
        optsdumped = data.readlines()[0]
        print optsdumped
        opts = cPickle.loads(optsdumped)
        print opts
        variable = StringVar(self.master)
        variable.set("one") # default value
        self.w = OptionMenu(self.master, variable, "one", "two", "three").grid(row = 8, sticky = W)
        #self.w.grid(row = 7, sticky = W)
        print "optionmenu"
    self.l0 =Label (self.master,fg = "blue", text = "W=jump", bg = "magenta", font = ("comic sans ms", 20, "bold")).grid(row=0, sticky = W)
    self.l1=Label (self.master,fg = "blue", text = "A=left",bg = "magenta",font = ("comic sans ms", 20, "bold")).grid(row = 1, sticky = W)
    self.l2=Label(self.master,fg = "blue", text = "D=Duck", bg = "magenta",font = ("comic sans ms", 20, "bold")).grid(row=2, sticky = W )
    self.l3=Label(self.master,fg = "blue", text = "Mouse = Shoot", bg = "magenta",font = ("comic sans ms", 20, "bold")).grid(row=3, sticky = W)
    self.l4=Label(self.master,fg = "blue", text = "S=Duck", bg = "magenta",font = ("comic sans ms", 20, "bold")).grid(row=4, sticky = W)
    self.l5=Label(self.master,fg = "blue", text="Seed:", bg = "magenta",font = ("comic sans ms", 20, "bold")).grid(row=5, sticky = W)
    self.master.minsize(50, 50)
    self.master.weight = 2
    self.master.title("Fluffocalypse")
    print self.master.configure(bg = "magenta")
    self.e1 = Entry(self.master)
    self.e1.grid(row=5, column=1, sticky = W)
    self.b1 =Button(self.master, text = "Start", command = self.startGameNormally).grid(row = 6, sticky = W)
    self.b2 = Button(self.master, text = "alten Spielstand laden", command =self.loadOldGame).grid(row = 7, sticky = W)
    mainloop()

好吧,窗口就在那里,但不知何故,ifclause 永远不会满足,myData 显然是空的,即使我手动打开它也是如此。这是为什么?

4

2 回答 2

2

w每次运行程序时,您都会打开带有标志的文件。这会破坏上一次运行保存的数据。使用附加模式。

于 2013-03-10T23:29:46.213 回答
2

您永远不会显式关闭文件,因此它的内容可能从未真正写入。您需要显式saved.close()或最好使用上下文管理器,例如:

with open(filename, "w") as saved: 
    pickle.dump( lis1,saved, 2)
    try:
        data.write(filename+"\n")
    except Exception as e:
        print e

data即使它只是为了阅读而打开,也这样做是个好主意。

于 2013-03-10T22:35:31.273 回答