niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
getting user's home directory on windows
Post new topic   Reply to topic Page 1 of 1 [1 Post] View previous topic :: View next topic
Author Message
Sreeram Kandallu
*nix forums addict


Joined: 02 Jun 2006
Posts: 58

PostPosted: Tue Jul 18, 2006 3:33 pm    Post subject: getting user's home directory on windows Reply with quote

Hi everybody,

I'm having trouble using os.path.expanduser('~') on windows. It uses
$HOME or ($HOMEDRIVE+$HOMEPATH), 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.expanduser( '~' )

def valid(path) :
if path and os.path.isdir(path) :
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('HOMEPATH'))
if not valid(homeDir) :
homeDir = env( 'SYSTEMDRIVE' )
if homeDir and (not homeDir.endswith('\\')) :
homeDir += '\\'
if not valid(homeDir) :
homeDir = 'C:\\'
return homeDir

[sreeram;]

ps:
A bit of googling around got me this informative post:
http://www.mail-archive.com/bug-cvs@gnu.org/msg05565.html
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [1 Post] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 6:52 am | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts postfix - mail directory bandito Postfix 0 Tue Dec 09, 2008 11:13 pm
No new posts 2.2.4 mod_authz_owner "forgets" user's auth TJB Apache 0 Fri May 04, 2007 6:44 pm
No new posts Home based Internet research Jobs honey PHP 0 Fri Jul 21, 2006 11:29 am
No new posts problem with windows installer Christian Rengstl PostgreSQL 0 Fri Jul 21, 2006 10:55 am
No new posts Problem with Win32-SerialPort over bluetooth @ windows + ... ctloh Perl 0 Fri Jul 21, 2006 8:08 am

Repair Bad Credit | Internet Dating | Loans | Ringtones | Credit Counseling
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1197s ][ Queries: 16 (0.0566s) ][ GZIP on - Debug on ]