where should config files go in Windows?

Josiah Carlson jcarlson at nospam.uci.edu
Mon Feb 2 19:00:38 EST 2004


I use the following for multi-platform home directories in my own 
project http://pype.sourceforge.net

I've had no complaints from people using *nix or Windows.  I haven't 
heard of any users on macs yet, so have no comment.

  - Josiah


default_homedir = os.path.dirname(os.path.abspath(__file__))
dotpath = '.application_name'

try:
     #all user-based OSes
     thd = os.path.expanduser("~")
     if thd == "~": raise
     homedir = os.path.join(thd, dotpath)
except:
     try:
         #*nix fallback
         homedir = os.path.join(os.environ['HOME'], dotpath)
     except:
         try:
             #windows NT,2k,XP,etc. fallback
             homedir = os.path.join(os.environ['USERPROFILE'], dotpath)
         except:
             #What os are people using?
             homedir = os.path.join(default_homedir, dotpath)
try:
     # create the config directory if it
     # doesn't already exist
     def expandfull(var, rem=3):
         if not rem:
             return os.path.expandvars(var)
         a = os.path.expandvars(var)
         b = []
         d = [b.extend(i.split('\\')) for i in a.split('/')]
         c = []
         for i in b:
             if '%' in i:
                 c.append(expandfull(i, rem-1))
             else:
                 c.append(i)
         return '\\'.join(c)
     if eol == "\r\n" and '%' in homedir:
         homedir = expandfull(homedir)
     if not os.path.exists(homedir):
         os.mkdir(homedir)
except:
     #print "unable to create config directory", homedir
     homedir = default_homedir



More information about the Python-list mailing list