In one of my games, I need to append to the end of the game saves file if I the user is new or change the balance in the file if the user already has a game save. This requires me to open the file separately in write and append modes. Is there a way I could do this sumultaneously?
def write_to_txt(self):
if self.saved_game:
with open("Game Saves.txt", "w") as f:
new_saved_game = self.list_saved_game[0] + self.list_saved_game[1][:10] + str(self.balance) + "\n"
f.write(''.join(self.contents_of_txt_file).replace(self.saved_game, new_saved_game))
else:
with open("Game Saves.txt", "a") as f:
f.write("User: {}\nBalance = {}\n".format(self.name, self.balance))