Python Forum
How to save files in a separate directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save files in a separate directory
#1
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.

 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?
Reply


Messages In This Thread
How to save files in a separate directory - by Scordomaniac - Mar-08-2022, 10:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Filer and sort files by modification time in a directory tester_V 5 499 May-02-2024, 05:39 PM
Last Post: tester_V
  Loop through all files in a directory? Winfried 10 763 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 567 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  how to save to multiple locations during save cubangt 1 631 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  change directory of save of python files akbarza 3 1,066 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,800 Jun-27-2023, 01:17 PM
Last Post: diver999
  does not save in other path than opened files before icode 3 1,039 Jun-23-2023, 07:25 PM
Last Post: snippsat
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,782 May-07-2023, 12:33 PM
Last Post: deanhystad
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,989 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Read directory listing of files and parse out the highest number? cubangt 5 2,561 Sep-28-2022, 10:15 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020