Finding user's home dir

Michael mogmios at mlug.missouri.edu
Thu Feb 3 12:14:51 EST 2005


My own, less than perfect, way of finding the users home directory is 
like this:

def personal_directory ( default = None ):
    pdir = None
    if sys.platform.startswith ( 'win' ):
        import _winreg
        reg = _winreg.ConnectRegistry ( None, _winreg.HKEY_CURRENT_USER )
        pkey = _winreg.OpenKey ( reg, 
r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" )
        pdir = _winreg.QueryValueEx ( pkey, 'Personal' )[0]
        _winreg.CloseKey ( pkey )
        _winreg.CloseKey ( reg )
    else:
        pdir = os.getenv ( 'HOME' )
    if not pdir:
        pdir = default
    return pdir

--
Michael <mogmios at mlug.missouri.edu>
http://kavlon.org




More information about the Python-list mailing list