I am trying to create a program that takes in a username and high score, if they are already a user they update to their new high score or just adds the high score if not.
My code is:
try:
a = open("data", "r+")
except FileNotFoundError:
a = open("data", "w")
a = open("data", "r+")
b = a.read()
user = input("Username: ")
user2 = list(user)
if user in b:
old = input("What is your old highscore? ")
new = input("What is your new highscore? ")
b2 = b.split()
for line in b2:
#Where I want to edit.
line=line.replace(old, new)
print(line)
else:
new = input("What is your highscore? ")
a.write(user + " " + new + "\n")
a.close()
Does anyone know how to replace the old with the new in the file?
The simple answer is: it is impossible. Operating-systems and their file-operations have no notion of "lines". They deal with blocks of binary data. Some libraries such as Python's standard-library put a convenient abstraction for reading lines above this - but they don't allow you to address individual lines.
So how to solve the problem? Simply by opening the file, reading all lines, manipulating the line in question in place, and then write the whole file out again.
First off, after
write
See where that takes you.
I'd advise you to move to some standard format of saving information, such as JSON, YAML, XML, CSV, pickle or another. Then what you need is to read and parse the file into native data structure (probably
dict
in the case), modify it (it is trivial), and write it back.Example with
json
(human readable, quite easy to use):Example with
shelve
(not human-readable, but extremely easy to use):