User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 402,860 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,896 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 621 | Replies: 3
Reply
Join Date: Oct 2006
Location: Ohio
Posts: 39
Reputation: mruane is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mruane mruane is offline Offline
Light Poster

def save(): print "HOW DO I DO THIS?"

  #1  
Oct 31st, 2007
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Posts: 408
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 56
woooee woooee is offline Offline
Posting Pro in Training

Re: def save(): print "HOW DO I DO THIS?"

  #2  
Nov 2nd, 2007
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
Reply With Quote  
Join Date: Oct 2007
Posts: 7
Reputation: animatinator is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
animatinator animatinator is offline Offline
Newbie Poster

Re: def save(): print "HOW DO I DO THIS?"

  #3  
Nov 3rd, 2007
Here's an example in which the user types in text which is then saved to a file:
  1. ## The save function
  2. def SaveText(text):
  3. filepath = "Your filepath eg. C:\\Random files\\New text.txt"
  4. # Open the file first (this creates the file if it doesn't exist already)
  5. # It takes two variables; the file to save to (must contain the file
  6. # extension) and a string which can be "r", "w" or "r+". In this case
  7. # use "w", which means "write".
  8. infile = open("filepath", "w")
  9. # Write the text
  10. infile.write(text)
  11. # Close the file
  12. infile.close()
  13.  
  14. print "Text saving demonstration"
  15. print
  16. print
  17. text = raw_input("Enter some text: ")
  18. SaveText(text)
  19. print
  20. 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.
AHAHAHA, I'm such a 1337 haxxor! Whoever's at 127.0.0.1 just got their hard disk erased!
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: def save(): print "HOW DO I DO THIS?"

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

Jeff
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 1:26 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC