I have been trying to figure out how to create a custom save script for text games and a text editor that I am working on. I was thinking I would have to have the script write to the actual program, unless I had an extra file just for saves. Which would be the best route, if any of these two at all?

I am guessing that there are no replys here because the problem was not explained well enough. Perhaps a simple step by step example or some pseudo-code will garner more response. Example:
User enters data
Data is saved when user clicks save button

Here's an example in which the user types in text which is then saved to a file:

## The save function
def SaveText(text):
	filepath = "Your filepath eg. C:\\Random files\\New text.txt"
	# Open the file first (this creates the file if it doesn't exist already)
	# It takes two variables; the file to save to (must contain the file
	# extension) and a string which can be "r", "w" or "r+". In this case
	# use "w", which means "write".
	infile = open("filepath", "w")
	# Write the text
	# Close the file

print "Text saving demonstration"
text = raw_input("Enter some text: ")
print "Saved"

If you use this method, the second time you run the program it will overwrite the text you saved before; as it's writing to the same file. If you're making a text editor you'll need to implement a save dialog box that lets you choose where to save it so that this doesn't happen; how you can do that depends on what GUI module you use.

Also, check out the cPickle module, which automagically serializes data for you into a file which then can be read back at will.


This article has been dead for over six months. Start a new discussion instead.