Is there a way to get the "Saves" folder from within Python or the SDK?

General Tso

Panzer General
Joined
Oct 12, 2007
Messages
1,548
Location
U. S. of A.
My mod autosaves the game at certain points and I would like to direct these saves to a folder within the Civ4 game Saves folder. This is the folder that contains the "single" and "WorldBuilder" folders among others and is located in my documents folder as "My Games\Beyond the Sword\Saves". Unfortunately since the path to this folder might be different for different versions of Windows and it also includes the user name, I'm not sure how to find it within the game. I would prefer to use a solution from within Civ4 if possible. If that's not possible I can try a generic Python solution that finds the user's document folder on any version of Windows. However, I'm not sure that would work on every Civ4 setup out there so I would prefer using something internal to Civ4. Does anyone have any ideas?
 
I probably misspoke when I used the term autosaves. I would like to save the game when I want it saved and I would like to save it in a folder within the "Saves" folder. The game will be automatically saved as far as the player is concerned.
 
If you're using BUG you can use BugPath.getRootDir():

Code:
import BugPath

...

savePath = BugPath.getRootDir() + "/Saves/Tso"

If you're not using BUG, check out BugPath.py for code that will help you get what you want.
 
Thank you EmperorFool. While I'm not using bug, I believe your link to BugPath.py provided the information need to solve the problem.

Here's what I ended up with and it works fine on my computer:
folder = os.environ['USERPROFILE'] + "\Documents\My Games\Beyond the Sword\Saves"

However I have a few questions for EF (or anybody else that wants to add some input).

It looks like I would need to use the code from getUserDir instead of getRootDir as you listed in your example. or, am I missing something?

I tried using os.environ["HOME'] from the UserDir code in BugPath.py. Unfortunately, it didn't work on my computer (it gave a KEY error for 'HOME'). After examining os.environ I found that os.environ['USERPROFILE'] did what I wanted.

Here are my main two questions.

1. Is using USERPROFILE with os.environ a "proper" way to find the user's folder?

2. Will the "\Documents\My Games\Beyond the Sword\Saves" part in the line above work OK in all standard installations of Civ4?
 
BugPath has worked so far for people using Macs and Windows. Note that it only uses os.environ on a Mac. For Windows it uses the registry. On XP the name is "My Documents"--not "Documents". getUserDir() returns the My Documents folder while getRootDir() returns the folder within it that has the CivilizationIV.ini file. The latter is where the Saves folder is located.

getAppDir() or getExeDir() or something else like that returns the folder that holds the EXE. This isn't what you want but may be what you thought getRootDir() returned. I called the INI folder root to go alone with the /AltRoot Civ4 command-line option.
 
Thanks for the info. I added a check for Vista so that I can change the "Documents" folder name.
 
Back
Top Bottom