I'm currently stuck on a saving issue. I want to create levels for a game and I'd like to build and modify them in a pygame program, then save them in the form of a string in a .txt file. For now, I managed to open an existing file named "level_n.txt", delete the old level I want to overwrite, and write the new level matrix in the text file.
At this point, everything works perfectly.
If I modify the text file, close it, re-open it in the same program run, and print the level, the changes were saved.
Here is my problem: when I close my whole program and re-open it, the level isn't changed, and when I open the text file of the level manually, the changes aren't here either.
So from my perspective, it looks like Python creates a copy of the file in the RAM, overwrites it, uses it, but when it closes, the text file doesn't save.
I've included the part where I write the text, and a level example just in case. (I've tried to write the text using w, w+, r+, a+, none of these solve my issue)
if event.type == pygame.KEYDOWN: #checks for save
if event.key == pygame.K_s:
with open('level'+str(n+3)+'.txt', 'w+') as f:
f.truncate(0)
for i in range(0,len(level)):
lin = ""
for j in range(0,len(level[0])):
lin = lin + str(level[i][j][0])+","+str(level[i][j][1]) + "."
lin = lin[:-1]+"\n"
f.writelines(lin)
with open('level'+str(n+3)+'.txt', 'r') as g:
level = [[[int(num[0]),int(num[2])] for num in line.split('.')] for line in g]
print(level)
print("contents saved ! (maybe)")
Inside "level4.txt"
1,3.1,3.1,3.1,3.1,3
1,2.0,0.0,0.0,0.1,2
1,2.0,0.1,1.0,0.1,2
1,2.0,0.0,0.0,0.1,2
1,3.1,3.1,3.1,3.1,3
Python 3.8, window 10