0

I've made a game and I'd like to save the highscore. So I need a place to do that. I chose to put it in the C:\All Programs directory. My problem is that that directory name isn't the same on every computer. For example on mine it's C:/Program Files (x86).

So my question: Is there a way, to discover that path on any computer?

PROBLEM SOLVED:
os.getenv('PROGRAMFILES')

  • It doesn't look like you've done much research. I suggest looking up `Python Relative Paths` – Elias Benevedes Dec 03 '14 at 17:40
  • I didn't find anything - only ppl who want to find out their scripts file. But I want to get the normal programs file like C:/Program Files/ – David Jandrey Dec 03 '14 at 17:48
  • http://stackoverflow.com/questions/918154/relative-paths-in-python First result on that exact search, seems very relevant to me. – Elias Benevedes Dec 03 '14 at 17:49
  • @EliasBenevedes - how does a relative paths search help on a question about absolute paths? – tdelaney Dec 03 '14 at 18:04
  • @EliasBenevedes, that link appears to be unrelated; I believe you've misunderstood the question. – Nye Dec 03 '14 at 18:07
  • I understood it as a question on how to store files along with a program, and I felt relative paths would be a good way to approach this. Sorry if I mis understood the question. – Elias Benevedes Dec 03 '14 at 18:12

2 Answers2

2

I second @iCodez's answer to use os.getenvto get the path string from a system environment variable, but you might want to use the paths defined for APPDATA or LOCALAPPDATA instead.

Windows permissions settings on the Program Files directory may prevent a standard user account from writing data to the directory.

I believe the APPDATA and LOCALAPPDATA paths were designed for just such a use. On my system, APPDATA = C:\Users\myname\AppData\Roaming and LOCALAPPDATA = C:\Users\myname\AppData\Local. My user account has full read/write permission for both directories.

psteiner
  • 187
  • 3
  • 17
0

You could use os.getenv or os.environ:

>>> import os
>>>> os.getenv('PROGRAMFILES')
'C:\\Program Files'
>>> os.environ['PROGRAMFILES']
'C:\\Program Files'
>>>

Note that you can also specify a default return value for when an environment variable is not set:

>>> os.getenv('BADVAR', 'default')
'default'
>>> os.environ.get('BADVAR', 'default')
'default'
>>>
  • os.getenv('PROGRAMFILES') is exactly the thing I was looking for. Thank you – David Jandrey Dec 03 '14 at 19:24
  • Happy to have been of help! Please do not forget to accept an answer by clicking the check next to it. Doing so lets people know that this problem is solved. –  Dec 03 '14 at 19:30