Anuj Choudhry
Anuj Choudhry

Reputation: 43

How to rewrite a text file in Python

So if I have a code like this:

f = open('score.txt','r+')
playersScore = 1
oldscore = (f.read())
oldscore = int(oldscore)
score = oldscore + PlayersScore
f.write(str(score))
f.close()

The text file has 1 in it. If I try run the code it gives this error:

oldscore = int(oldscore)
ValueError: invalid literal for int() with base 10: ''

How can I fix this? I'm a python beginner so please keep it simple.

Upvotes: 3

Views: 9898

Answers (6)

Peter
Peter

Reputation: 577

I think the problem is caused by adding a new line instead of replacing the old line. This should work:

f = open('score.txt','r+')
playersScore = 1
oldscore = (f.read())
oldscore = int(oldscore)
score = oldscore + playersScore
f.seek(0)
f.write(str(score))
f.close()

Upvotes: 3

George Willcox
George Willcox

Reputation: 673

The error you got is because the file is empty or contains alpha characters (letters), I don't know why. Something else that's wrong, is that opening a file in mode r+, doesn't rewrite the file. You need to read the file, and then overwrite it.

f = open('score.txt', 'r')
playerScore = 1
oldScore = int(f.read())
f.close()
f = open('score.txt', 'w')
score = oldScore + playerScore
f.write(str(score))
f.close()

Upvotes: 0

Zizouz212
Zizouz212

Reputation: 4998

You can see the values of what you are trying to do by printing them periodically.

f = open('score.txt','r+')
playersScore = 1
oldscore = (f.read())
print(oldscore) # Let's see what the value of this is!
oldscore = int(oldscore)
score = oldscore + playersScore
f.write(str(score))
f.close()

Even if the only contents of the file may look like something such as 3, it may have a line break at the end (\n). You might want to get rid of that.

The .strip() method for strings is a good bet. It will get rid of line breaks at the beginning or end of the string.

This should be some working code:

f = open('score.txt','r+')
playersScore = 1
oldscore = (f.read())
print(oldscore) # Let's see what the value of this is!
oldscore = int(oldscore.strip()) # See how we called .strip()?
score = oldscore + playersScore
f.write(str(score))
f.close()

Upvotes: 3

Garrett Ellis
Garrett Ellis

Reputation: 111

The above comment from Zizouz212 nailed it; your score.txt file contains additional characters, such as a newline, which cannot be converted to an integer. When populating the 1 in score.txt, you might have pressed the enter key before saving it.

Using .strip() on the value read from the line ensures that int() only receives the value from the line.

Also, your code sample capitalizes PlayersScore when adding it with oldscore. This is most likely a type-o in your post, but if not, this will also produce problems when running your program.

Upvotes: 2

jacmoe
jacmoe

Reputation: 1687

This line:

score = oldscore + PlayersScore

should be:

score = oldscore + playersScore

Try that.

Upvotes: 0

user2255757
user2255757

Reputation: 766

playerscore = 1
for line in open('score.txt'):
    playerscore += int(line)

Upvotes: -2

Related Questions