How to load file and create infinite save files? How to load file and create infinite save files? tkinter tkinter

How to load file and create infinite save files?


Saving is easy. You have many options, but two of the primary ones are:

1) The pickle module

https://docs.python.org/2/library/pickle.html

Built (by default, in Python3x) on C code, this is a very fast way to serialize objects and recover them. Use unique names for files, and look at the "dump" and "load" methods.

From the docs, this example should get you started:

# Save a dictionary into a pickle file.import picklefavorite_color = { "lion": "yellow", "kitty": "red" }pickle.dump( favorite_color, open( "save.p", "wb" ) )# Load the dictionary back from the pickle file.favorite_color = pickle.load( open( "save.p", "rb" ) )# favorite_color is now { "lion": "yellow", "kitty": "red" }

In tkinter, this (the tkFileDialog)

http://tkinter.unpythonic.net/wiki/tkFileDialog

Should help you make a dialog for selecting file locations. Here's a good example of its usage:

Opening File (Tkinter)

2) Load/save and parse files yourself

You said that your game is for learning purposes, so doing things through manual file io isn't a bad idea. The documentation has a good place to get started, using "open" as the primary function for handling files. Again, "infinite" files simply means using unique name for each one

https://docs.python.org/2/tutorial/inputoutput.html

An example of manual io is

# writing data to a filefavorite_colors = {'tim':'yellow','mary':'blue'}newsave_file = open(filename,'w')for key, val in favorite_colors.items():    newsave_file.write(str(key)+'|'+str(val)+'\n')newsave_file.close()# reading data from a filefavorite_colors = {}open_file = open(filename,'r')for line in open_file:    pair = line.split('|')    favorite_colors[pair[0]] = pair[1]open_file.close()

You may want to do things like using try/catch block to ensure the program doesn't crash, or a more sophisticated parser technique. That's all up to you!