How do you easily save over a .txt file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raubana
    New Member
    • Feb 2008
    • 55

    How do you easily save over a .txt file?

    What I'm trying to do is make a highscore system for a game I made (see one of my prev. posts). What it does is reads the file when it starts and saves when the program is shut down, but I found it really tricky because using
    [CODE=python]
    info = file("___.txt")
    info.seek(0)

    info.write(str( highscore))
    [/CODE]
    is very uneffective. This is because it just moves the text instead of writing over it.

    Is it possable to make a huge string and just tell the file that's what it will now be, or how about removeing all the files strings so I can write in the new highscores?


    OH! How about saving directly to the .py file, is that possable? If so, would it still work if I turned it into a .exe file?
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    When you open a file in Python (use the open(filename, mode) function, rather than the file constructor), the second parameter is 'w', 'r', 'a', or any of those with a 'b' after. Respectively, they mean 'write', 'read', and 'append'. (The 'b' means binary mode). Write mode erases any existing text before you start reading, read mode is read-only, and append mode lets you write to the file from where the existing text ends.

    Comment

    • raubana
      New Member
      • Feb 2008
      • 55

      #3
      I don't understand. When I tell it to write, it just moves the text.

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        Originally posted by raubana
        I don't understand. When I tell it to write, it just moves the text.
        [code=python]
        fh = open('somefile. txt', 'w') # This will erase the contents of the file and allow you to write
        fh.write('This is some text in the file %s\n' % fh.name)
        fh.close() # Closes the file handle

        fh = open('somefile. txt', 'r') # This opens the file for reading
        content = fh.read() # Reads all the contents of the file ...
        fh.seek(0) # sets the cursor back to beginning of file
        for line in fh: # This iterates through each line of the file
        print line # Print the line

        fh.close() # Closes the file handle[/code]

        P.S. Mods/Admins: What happened to the python code tags? They don't work anymore! code=python just shows up as generic text! I don't know if you guys have noticed this or not but it bugs me!

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by jlm699
          P.S. Mods/Admins: What happened to the python code tags? They don't work anymore! code=python just shows up as generic text! I don't know if you guys have noticed this or not but it bugs me!
          I have noticed it, and it bugs me too!

          Comment

          • raubana
            New Member
            • Feb 2008
            • 55

            #6
            Not you too! So I'm not the only one...

            Anyway, here's the code (it's a zip file)
            It has all the stuff so you can look at the code, and it also has a before and after save versions of highscores.ads (the extention hides the fact it's really a text file). Tell me what you can do to help, please! ; )

            Okay...for some reason it won't upload the file... Hold on.

            Comment

            • raubana
              New Member
              • Feb 2008
              • 55

              #7
              Here it is:

              Adsector

              Comment

              • jlm699
                Contributor
                • Jul 2007
                • 314

                #8
                After you read in the information save it to a variable and close your file handle with fh.close()

                Then open the file once again but with the 'w' mode. Then simply write the information to file, clearing the previous contents.

                Comment

                Working...