Mar-08-2022, 10:31 AM
Hey, i'm working on a games save/loading function.
this is my code that find the file path for the players save (loading the save)
it works by finding where the file is being run from, then replacing the program with the playerdata subfolder so it can find the save files.
this is an example of code I would use to save files if they were in the same directory as the program.
TL;DR it deletes the text files and replaces them with a new save - but as the files are in a subfolder it cannot locate them - how do i get it to save to a certain directory?
this is my code that find the file path for the players save (loading the save)
it works by finding where the file is being run from, then replacing the program with the playerdata subfolder so it can find the save files.
absolutepath = os.path.abspath(__file__) filename = os.path.basename(absolutepath) dirname = absolutepath.replace(filename, "playerdata") filename = dirname + "\\username.txt" file = open(filename, 'r') player = file.read() file.close() absolutepath = os.path.abspath(__file__) filename = os.path.basename(absolutepath) dirname = absolutepath.replace(filename, "playerdata") filename = dirname + "\\coins.txt" file = open(filename, 'r') coins = file.read() file.close()how can I use this system to save the file?
this is an example of code I would use to save files if they were in the same directory as the program.
os.remove('username.txt') file = open('username.txt', 'a+') file.write(player) file.close() os.remove('coins.txt') file = open('coins.txt', 'a+') coins = str(coins) file.write(coins) file.close()
TL;DR it deletes the text files and replaces them with a new save - but as the files are in a subfolder it cannot locate them - how do i get it to save to a certain directory?