Finding where to store application data portably

T

Tony Houghton

I'm using pygame to write a game called Bombz which needs to save some
data in a directory associated with it. In Unix/Linux I'd probably use
"~/.bombz", in Windows something like
"C:\Documents And Settings\<user>\Applicacation Data\Bombz".

There are plenty of messages in the archives for this group about how to
find the correct location in Windows, but what about Mac OS? There I
don't know the correct location for this sort of thing at all. And there
are other, more obscure systems like RISC OS (it may not have pygame but
it definitely has python). Surely this is something that's crying out
for an official function in os or sys.
 
R

Robert Kern

Tony said:
I'm using pygame to write a game called Bombz which needs to save some
data in a directory associated with it. In Unix/Linux I'd probably use
"~/.bombz", in Windows something like
"C:\Documents And Settings\<user>\Applicacation Data\Bombz".

There are plenty of messages in the archives for this group about how to
find the correct location in Windows, but what about Mac OS? There I
don't know the correct location for this sort of thing at all. And there
are other, more obscure systems like RISC OS (it may not have pygame but
it definitely has python). Surely this is something that's crying out
for an official function in os or sys.

On OS X, the data should probably go to
~/Library/Application Support/Bombz/

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
P

Peter Hansen

Tony said:
I'm using pygame to write a game called Bombz which needs to save some
data in a directory associated with it. In Unix/Linux I'd probably use
"~/.bombz", in Windows something like
"C:\Documents And Settings\<user>\Applicacation Data\Bombz".

There are plenty of messages in the archives for this group about how to
find the correct location in Windows, but what about Mac OS? There I
don't know the correct location for this sort of thing at all. And there
are other, more obscure systems like RISC OS (it may not have pygame but
it definitely has python). Surely this is something that's crying out
for an official function in os or sys.

Perhaps using "import user; user.home" would be adequate. Note the
documented side effects of doing that however.

-Peter
 
S

Steven D'Aprano

I'm using pygame to write a game called Bombz which needs to save some
data in a directory associated with it. In Unix/Linux I'd probably use
"~/.bombz", in Windows something like
"C:\Documents And Settings\<user>\Applicacation Data\Bombz".

In Windows, you shouldn't hard-code the drive letter. I don't know how you
find out what the correct value is, but hard-coding it is just Bad.

As a Linux user, I really am sick of every damn application, script and
program under the sun filling the top level of my home directory with
dot-files.

I wish the Linux Standard Base folks would specify that settings files
should all go into a subdirectory like ~/settings rather than filling up
the home directory with cruft. That was acceptable in the days when people
only looked at their files with ls, but in these days of GUI file
managers, it is ridiculous that there are more than 100 dot files and
directories in my home directory.

<tilting at windmills>

Can I ask developers to break with the obsolete and annoying habit of
creating user-specific config files as ~/.app-name and use
~/settings/app-name instead?

</tilting at windmills>
 
S

Steve Holden

Steven said:
In Windows, you shouldn't hard-code the drive letter. I don't know how you
find out what the correct value is, but hard-coding it is just Bad.

As a Linux user, I really am sick of every damn application, script and
program under the sun filling the top level of my home directory with
dot-files.

I wish the Linux Standard Base folks would specify that settings files
should all go into a subdirectory like ~/settings rather than filling up
the home directory with cruft. That was acceptable in the days when people
only looked at their files with ls, but in these days of GUI file
managers, it is ridiculous that there are more than 100 dot files and
directories in my home directory.

<tilting at windmills>

Can I ask developers to break with the obsolete and annoying habit of
creating user-specific config files as ~/.app-name and use
~/settings/app-name instead?

</tilting at windmills>
While we're asking for the improbable, perhaps we could make that

~/.settings/app-name

Ssh has used the ~/.ssh directory for a long time to avoid the need for
lots of little dot files, and this works quite well. Good luck in your
campaign!

regards
Steve
 
D

Dennis Lee Bieber

In Windows, you shouldn't hard-code the drive letter. I don't know how you
find out what the correct value is, but hard-coding it is just Bad.
XP Pro seems to take
C:\Documents and Settings\Dennis Lee Bieber\Application Data

Though the more consistent might be to hit the registry:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders\AppData

and to confuse matters, there is also a "Local AppData" entry
--
 
J

Jeff Schwab

Steven said:
As a Linux user, I really am sick of every damn application, script and
program under the sun filling the top level of my home directory with
dot-files.

I wish the Linux Standard Base folks would specify that settings files
should all go into a subdirectory like ~/settings rather than filling up
the home directory with cruft. That was acceptable in the days when people
only looked at their files with ls, but in these days of GUI file
managers, it is ridiculous that there are more than 100 dot files and
directories in my home directory.

What difference does it make whether one is using a GUI or a command
line? Anyway, the vast majority of Unix users I know still use ls a lot
more often than GUI-based file managers.

I prefer to keep my settings in ~/etc, as a sort of intuitive parallel
to the Filesystem Hierarchy Standard.
<tilting at windmills>

Can I ask developers to break with the obsolete and annoying habit of
creating user-specific config files as ~/.app-name and use
~/settings/app-name instead?

</tilting at windmills>

Better: a user-specified $ETCDIR.
 
T

Trent Mick

I'm using pygame to write a game called Bombz which needs to save some
In Windows, you shouldn't hard-code the drive letter. I don't know how you
find out what the correct value is, but hard-coding it is just Bad.
XP Pro seems to take
print os.environ["APPDATA"]
C:\Documents and Settings\Dennis Lee Bieber\Application Data

Though the more consistent might be to hit the registry:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders\AppData

and to confuse matters, there is also a "Local AppData" entry

It isn't just the drive letter you shouldn't hardcode. In different
locales the phrases "Documents and Settings" and "Application Data"
are often different. The canonical way to get this dir on Windows is by
using the SHGetFolderPath Windows API call with the CSIDL_APPDATA
constant:

http://msdn.microsoft.com/library/d...shell/reference/functions/shgetfolderpath.asp

In Python code that is:

from win32com.shell import shellcon, shell
path = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)

Unfortunately that either means having the PyWin32 extensions installed
(which not all Python installations on Windows have) or building your
own small binary extension to make that call. Failing that, using the
registry location or the environment variable above might suffice
Note that on some older Windows flavours -- not sure exactly which --
the APPDATA env. var. might not be defined.

Also note that Windows app guidelines encourage you to have an
additional vendor/owner directory level before the app name. So:

%APPDATA%\<company-or-vendor-name>\Bombz

I have a little working script that I use for this kind of stuff which
you are welcome too, if it helps. (attached)

Trent
 
T

Tony Houghton

Steven said:
> On Tue, 20 Sep 2005 23:03:52 +0100, Tony Houghton wrote:
>
>
>
> In Windows, you shouldn't hard-code the drive letter. I don't know how you
> find out what the correct value is, but hard-coding it is just Bad.

That's why I said "something like". It seems there is a definitive way
of finding the correct value, but it needs extra W32 extensions
installed. You could also start from os.path.expanduser('~') I suppose.
> As a Linux user, I really am sick of every damn application, script and
> program under the sun filling the top level of my home directory with
> dot-files.
>
> I wish the Linux Standard Base folks would specify that settings files
> should all go into a subdirectory like ~/settings rather than filling up
> the home directory with cruft. That was acceptable in the days when people
> only looked at their files with ls, but in these days of GUI file
> managers, it is ridiculous that there are more than 100 dot files and
> directories in my home directory.

Don't all file managers have an option to hide files beginning with '.'?
> <tilting at windmills>
>
> Can I ask developers to break with the obsolete and annoying habit of
> creating user-specific config files as ~/.app-name and use
> ~/settings/app-name instead?
>
> </tilting at windmills>

You'll probably like the XDG basedir spec then:
<http://freedesktop.org/wiki/Standards_2fbasedir_2dspec>

I suppose I should really use that, but the trouble is it can be very
difficult to decide whether some files are config or data, so it would
be nice to have a standard that doesn't segregate the two.

Thanks to everyone else who's replied. I've saved Trent Mick's script
for later reference.
 
S

Steven D'Aprano

Don't all file managers have an option to hide files beginning with '.'?

I don't want to hide them. I just don't want them in my face when I open
my home directory.
 
R

Ron Adam

Steven said:
I don't want to hide them. I just don't want them in my face when I open
my home directory.

+1

This has been a gripe of mine on windows as well, and hiding files (or
extensions) is definitely not the answer.

Personally I think hidden files do more harm than good. It's not a
substitute for good file management, and it not an acceptable
alternative to good security either.

Cheers,
Ron
 
R

Ron Adam

Tony said:
I'm using pygame to write a game called Bombz which needs to save some
data in a directory associated with it. In Unix/Linux I'd probably use
"~/.bombz", in Windows something like
"C:\Documents And Settings\<user>\Applicacation Data\Bombz".

There are plenty of messages in the archives for this group about how to
find the correct location in Windows, but what about Mac OS? There I
don't know the correct location for this sort of thing at all. And there
are other, more obscure systems like RISC OS (it may not have pygame but
it definitely has python). Surely this is something that's crying out
for an official function in os or sys.


This works on Win XP. Not sure if it will work on Linux.

import os

parent = os.path.split(os.path.abspath(os.sys.argv[0]))[0]
file = parent + os.sep + '.bombz'

Cheers,
Ron
 
S

Steven D'Aprano

+1

This has been a gripe of mine on windows as well, and hiding files (or
extensions) is definitely not the answer.

Personally I think hidden files do more harm than good. It's not a
substitute for good file management, and it not an acceptable
alternative to good security either.

Agreed. If "hidden files" are under the control of the user, then they
aren't really hidden, they are merely filtered from the current view.
Filtering may be useful in many circumstances, but if you are filtering
the same files all the time (eg "don't show me files starting with a
period" or "don't show me files with the invisible flag set") then you
should stick them in a subdirectory.

And if hidden files aren't under the control of the user (as certain
obscure ways of hiding files under Windows aren't), then it is a serious
security risk. You, the user might not be able to hide the files, but you
can bet some virus will. Eg if you drop an executable file in the Windows
XP font directory, it will not show up in the file explorer.
 
T

Tony Houghton

Ron said:
> Tony Houghton wrote:
>
>>
>> I'm using pygame to write a game called Bombz which needs to save some
>> data in a directory associated with it. In Unix/Linux I'd probably use
>> "~/.bombz", in Windows something like
>> "C:\Documents And Settings\<user>\Applicacation Data\Bombz".
>>
>> There are plenty of messages in the archives for this group about how to
>> find the correct location in Windows, but what about Mac OS? There I
>> don't know the correct location for this sort of thing at all. And there
>> are other, more obscure systems like RISC OS (it may not have pygame but
>> it definitely has python). Surely this is something that's crying out
>> for an official function in os or sys.
>
> This works on Win XP. Not sure if it will work on Linux.
>
> import os
>
> parent = os.path.split(os.path.abspath(os.sys.argv[0]))[0]
> file = parent + os.sep + '.bombz'

Ooh, no, I don't want saved data to go in the installation directory. In
general that practice encourages people to run with Admin access, and
it's about time Windows users were discouraged from that.
 
S

Steve Holden

Ron said:
Tony said:
I'm using pygame to write a game called Bombz which needs to save some
data in a directory associated with it. In Unix/Linux I'd probably use
"~/.bombz", in Windows something like
"C:\Documents And Settings\<user>\Applicacation Data\Bombz".

There are plenty of messages in the archives for this group about how to
find the correct location in Windows, but what about Mac OS? There I
don't know the correct location for this sort of thing at all. And there
are other, more obscure systems like RISC OS (it may not have pygame but
it definitely has python). Surely this is something that's crying out
for an official function in os or sys.



This works on Win XP. Not sure if it will work on Linux.

import os

parent = os.path.split(os.path.abspath(os.sys.argv[0]))[0]
file = parent + os.sep + '.bombz'

Cheers,
Ron
Since you've gone to the trouble to use os.path functions why not use

file = os.path.join(parent, 'bombz')

regards
Steve
 
R

rbt

I'm using pygame to write a game called Bombz which needs to save some
data in a directory associated with it. In Unix/Linux I'd probably use
"~/.bombz", in Windows something like
"C:\Documents And Settings\<user>\Applicacation Data\Bombz".

There are plenty of messages in the archives for this group about how to
find the correct location in Windows, but what about Mac OS?

~/.bombz works equally well on OSX.
 
M

Mike Meyer

rbt said:
~/.bombz works equally well on OSX.

But Mac users - as opposed to Unix geeks - will expect to find it in
~/Library/Application Support/Bombz.

Us unix geeks can symlink that to ~/.bombz if we use the application
on both platforms.

<mike
 
G

Guest

I wish the Linux Standard Base folks would specify that settings files
should all go into a subdirectory like ~/settings rather than filling up
the home directory with cruft. That was acceptable in the days when people
only looked at their files with ls, but in these days of GUI file
managers, it is ridiculous that there are more than 100 dot files and
directories in my home directory.

So it is the very rationale why dot-files historically considered as
"hidden" in all unix shells and filemanagers.
<tilting at windmills>

Can I ask developers to break with the obsolete and annoying habit of
creating user-specific config files as ~/.app-name and use
~/settings/app-name instead?

</tilting at windmills>

There is an other way around: look at your home dir as if it is your
"settings" dir and don't clutter it with files other than application
config dot-files. Just make ~/files/, ~/bin/ ~/lib/ etc. for it.
 
R

Ron Adam

Steve said:
Ron said:
Tony said:
I'm using pygame to write a game called Bombz which needs to save some
data in a directory associated with it. In Unix/Linux I'd probably use
"~/.bombz", in Windows something like
"C:\Documents And Settings\<user>\Applicacation Data\Bombz".

There are plenty of messages in the archives for this group about how to
find the correct location in Windows, but what about Mac OS? There I
don't know the correct location for this sort of thing at all. And there
are other, more obscure systems like RISC OS (it may not have pygame but
it definitely has python). Surely this is something that's crying out
for an official function in os or sys.




This works on Win XP. Not sure if it will work on Linux.

import os

parent = os.path.split(os.path.abspath(os.sys.argv[0]))[0]
file = parent + os.sep + '.bombz'

Cheers,
Ron
Since you've gone to the trouble to use os.path functions why not use

file = os.path.join(parent, 'bombz')

It just didn't come to mind first. I don't use os.path that often.

Thanks. :)
 
R

Ron Adam

Tony said:
This works on Win XP. Not sure if it will work on Linux.

import os

parent = os.path.split(os.path.abspath(os.sys.argv[0]))[0]
file = parent + os.sep + '.bombz'

Ooh, no, I don't want saved data to go in the installation directory. In
general that practice encourages people to run with Admin access, and
it's about time Windows users were discouraged from that.

Yes, it occurred to me you didn't want to do that after I posted.

Looks like maybe the correct place would be as you suggested, but maybe
doing it this way would be better.

import os
user = os.path.join( os.environ["USERPROFILE"],
'Application Data',
'Bombz' )


Cheers,
Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,005
Latest member
ChasityFan

Latest Threads

Top