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?

Recommended Answers

All 3 Replies

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
etc

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
	infile.write(text)
	# Close the file
	infile.close()

print "Text saving demonstration"
print
print
text = raw_input("Enter some text: ")
SaveText(text)
print
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.

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.