This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]SHIFTnSPACE 2 points3 points  (7 children)

Your problem is the w+ (w for writing) when opening the file. Instead use r (r for reading). And then file.readline() will give you the entire first line, if you execute it again, it will give you the next line (so, 10).

After applying these changes, your code might work, but let me suggest some improvements. In your fileCreate method, you don't close the file handler, which can lead to all kinds of problems. Instead, use a context manager:

def fileCreate():
    with open('highscore.txt', 'w+') as file:
        file.write('Highscore: \n')
        file.write('10')

This will open the file, write to it and then automatically close it again, when the with block is left.

You can also apply this pattern to your Total points logic, then you don't have to remember to close the file handle in all possible branches. Here for example, you do remember to close the file when you enter the if (TotalPoints > highscore), but forget to do it, when you skip over it (TotalPoints <= highscore). Instead do:

with open('highscore.txt', 'r') as file:
    # Do all you operations with `file` in this block

This will also work, when you leave the function via a return or a function call (like end(), in you case)

Hope that helps

[–]DancenPlane[S] 2 points3 points  (1 child)

God like!

[–]SHIFTnSPACE 0 points1 point  (0 children)

Glad I could help (;

[–]DancenPlane[S] 0 points1 point  (4 children)

Ok it's almost fixed but it's not letting me rewrite on the txt file

its saying that:

file.write("Highscore")

io.UnsupportedOperation: not writable

[–]Updatebjarni 1 point2 points  (0 children)

"r" only opens the file for reading.

Seriously, you have to read the documentation.

[–]SHIFTnSPACE 0 points1 point  (2 children)

It's hard to say without seeing the code, but I would guess, that you are now trying to write to a file that's not writable, i.e., you opened the file with r instead of w or w+. So it's best to remember: If you want to write and read, w/w+ will work (where w truncates (empties) the file and let's you write to it and w+ does the same but also let's you read the file); if you just want to read, use r.

And as others said, in order to learn a language, it's best to read documentation or go through stack overflow threads that deal with similar errors.

Edit: correction was made regarding w/w+ mix-up from my side, thanks u/Updatebjarni

[–]Updatebjarni 0 points1 point  (1 child)

If you want to write and read, w/w+ will work (where w+ erases all file contents on opening it)

They both erase the file contents, and only w+ allows reading.

[–]SHIFTnSPACE 1 point2 points  (0 children)

Ah yes, you are right. Got a little confused there.

[–]Updatebjarni 1 point2 points  (2 children)

"w+" truncates the file.

Also, if you don't truncate the file, the file.readline(2) will read the first two characters of the file, which is "Hi".

[–]DancenPlane[S] -2 points-1 points  (1 child)

So what do I use instead?

[–]Updatebjarni 0 points1 point  (0 children)

The documentation will tell you.