views:

139

answers:

2

For some (small) programs I'm writing in Python, I'd like to be able to set, store and retrieve user preferences in a file in a portable (multi-platform) way.

I'm obviously thinking about a very simple ConfigParser file like "~/.program" or "~/.program/program.cfg".

Is os.path.expanduser() the best way for achieving this or there's something more easy/straightforward that I'm missing?

+5  A: 
os.path.expanduser("~")

is more portable than

os.environ['HOME']

so it should be ok to use the first.

The MYYN
os.path.expanduser('~') is a portable solution, but consider using xdg.BaseDirectory (http://freedesktop.org/wiki/Software/pyxdg) to find a proper location for your application specific data on platforms that follow freedesktop.org's XDG Base Directory Specification (http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html).
Andi Albrecht
I appreciate the FD standard, but this requires an external module, so I would really go for the stdlib solution.
steko
A: 

You can use os.environ:

import os
print os.environ["HOME"]