Skip to content
Advertisement

Loading a config file from operation system independent place in python

under Linux I put my configs in “~/.programname”. Where should I place it in windows? What would be the recommendated way of opening the config file OS independent in python?

Thanks! Nathan

Advertisement

Answer

Try:

os.path.expanduser('~/.programname')

On linux this will return:

>>> import os
>>> os.path.expanduser('~/.programname')
'/home/user/.programname'

On windows this will return:

>>> import os
>>> os.path.expanduser('~/.programname')
'C:\Documents and Settings\user/.programname'

Which is a little ugly, so you’ll probably want to do this:

>>> import os
>>> os.path.join(os.path.expanduser('~'), '.programname')
'C:\Documents and Settings\user\.programname'

EDIT: For what it’s worth, the following apps on my Windows machine create their config folders in my Documents and Settingsuser folder:

  • Android
  • AgroUML
  • Gimp
  • IPython

EDIT 2: Oh wow, I just noticed I put /user/.programname instead of /home/user/.programname for the linux example. Fixed.

Advertisement