3

I am making a jython application that needs a config/settings file (and probably eventually directory) but I want it be stored in the expected/correct directory for each os.

~/.app_name/config in linux

c:/documents and Settings/User/app_name ?? in windows.

I have found this:

http://snipplr.com/view/7354/home-directory-crossos/

but this is for python and I have feeling that this might not necessary work for jython/windows and I don't have any dev stuff set up in my windows VM to test at this moment

If someone could provide any insight into the "best practices" (for jython) for achieving this I would greatly appreciate it.

Thanks.

EDIT:

Here is what I have come up with that seems to be working...I would appreciate any feedback

import os
from java.lang import System
from java.io import File
os_name =  System.getProperty('os.name')
os_sep = File.separator
app_name = 'ctrlmac'
if os_name == 'Windows':
    config_dir = os.environ["APPDATA"] + os_sep + app_name
else:
    config_dir = os.path.expanduser("~") + os_sep + '.' + app_name
print config_dir
Jack
  • 20,735
  • 11
  • 48
  • 48
  • On Windows it is _not_ a directory right in the User's home. I hate applications that happily write their configuration there. The correct place would be Application data, either from the %AppData% and %LocalAppData% environment variables (not recommended) or via the appropriate API calls. – Joey Jul 05 '09 at 21:25
  • Thanks for the heads up Johannes. I hardly ever use Windows so this is nice to know. So are you saying something like this: >>> import os >>> os.environ["APPDATA"] 'C:\\Documents and Settings\\username\\Application Data' Thanks. – Jack Jul 05 '09 at 21:45
  • In principle, yes. But environment variables are not the proper way to find those folders out programmatically. I think the SHGetFolderPath (http://msdn.microsoft.com/en-us/library/bb762181.aspx) function with CSIDL_APPDATA is the recommended way of getting that path since yuo can't rely on environment variables being set. – Joey Jul 05 '09 at 22:11
  • Possible duplicate of [Is there a standard way to get the user config directory in python](https://stackoverflow.com/questions/31866115/is-there-a-standard-way-to-get-the-user-config-directory-in-python) – Victor Aurélio Mar 27 '19 at 16:49
  • @VictorAurélio this answer is almost 10 years old and the duplicate is almost 4 years old (and that one is a link only answer) – Jack Mar 27 '19 at 21:10
  • @Jack sorry for that, but does `appdirs` helps or solve your question? – Victor Aurélio Mar 30 '19 at 16:00

0 Answers0