getting user's home directory on windows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K.S.Sreeram

    getting user's home directory on windows

    Hi everybody,

    I'm having trouble using os.path.expandu ser('~') on windows. It uses
    $HOME or ($HOMEDRIVE+$HO MEPATH), but this doesn't work with windows
    machines which are part of a domain. On such machines, the HOME envvar
    may not be set at all, and the HOMEPATH envvar may be set to '\\'!!

    Here's an implementation of getHomeDir which tries to find the best
    possible option. I don't know how this'll behave on older versions of
    windows such as win2k or win98. Please let me know if anybody knows a
    better way to do this :

    def getHomeDir() :
    if sys.platform != 'win32' :
    return os.path.expandu ser( '~' )

    def valid(path) :
    if path and os.path.isdir(p ath) :
    return True
    return False
    def env(name) :
    return os.environ.get( name, '' )

    homeDir = env( 'USERPROFILE' )
    if not valid(homeDir) :
    homeDir = env( 'HOME' )
    if not valid(homeDir) :
    homeDir = '%s%s' % (env('HOMEDRIVE '),env('HOMEPAT H'))
    if not valid(homeDir) :
    homeDir = env( 'SYSTEMDRIVE' )
    if homeDir and (not homeDir.endswit h('\\')) :
    homeDir += '\\'
    if not valid(homeDir) :
    homeDir = 'C:\\'
    return homeDir

    [sreeram;]

    ps:
    A bit of googling around got me this informative post:



    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2.2 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFEvP5/rgn0plK5qqURAgW QAKCUfnBT4hBD14 i/rG5EXzzH0Dx/ygCgjHdc
    AnGSwSZ64odBbfn eQXw1mEg=
    =9UJP
    -----END PGP SIGNATURE-----

Working...