473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

getting user's home directory on windows

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/bu*****@.../msg05565.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEvP5/rgn0plK5qqURAgWQAKCUfnBT4hBD14i/rG5EXzzH0Dx/ygCgjHdc
AnGSwSZ64odBbfneQXw1mEg=
=9UJP
-----END PGP SIGNATURE-----

Jul 18 '06 #1
0 1042

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alec Wysoker | last post by:
Could you explain a little more clearly what the problem is? In the implementation of expanduser in Python 2.3.4, it uses the value of HOME env var if it exists, otherwise, it uses HOMEDRIVE +...
2
by: Sibylle Koczian | last post by:
Hello, I've installed Python 2.4 and the win32 extensions, using administrator rights, under Windows XP in "C:\Programme". As this is a directory without spaces I didn't expect any problems. But...
13
by: lawpoop | last post by:
Hello all - I have a two part question. First of all, I have a website under /home/user/www/. The index.php and all the other website pages are under /home/user/www/. For functions that are...
185
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.