Finding user's home dir
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 + HOMEPATH. I don't have access to a Win 2K machine right now, but my recollection is that when I did, HOMEDRIVE and HOMEPATH were set by Windows, so the right thing should happen. Do you not have HOMEDRIVE + HOMEPATH set in your environment, e.g. if you open a CMD.exe window? When you say that it returned %USERPROFILE%, do you mean that it returned that actual string, or the value of that env var? Or is it that you want it to return the path to your My Documents directory, which is not necessarily the same thing as %HOME%? Thanks, Alec > Hi all, I'm trying to write a multiplatform function that tries to > return the actual user home directory. I saw that > os.path.expanduser("~") works on Linux but on Windows2000 (at least on > the win I used) it returns %USERPROFILE%, so I tried os.environ["HOME"] > and it gave me the same results. So I ended up with > os.environ["USERPROFILE"], it doesn't work on Linux but (at least) on > Windows2000 it returns the correct information > > I googled a little bit and it seems that there is no general solution, > so I tried to merge what I found, and I wrote this little function: > > def getHomeDir(): > ''' Try to find user's home directory, otherwise return current > directory.''' > try: > path1=os.path.expanduser("~") > except: > path1="" > try: > path2=os.environ["HOME"] > except: > path2="" > try: > path3=os.environ["USERPROFILE"] > except: > path3="" > > if not os.path.exists(path1): > if not os.path.exists(path2): > if not os.path.exists(path3): > return os.getcwd() > else: return path3 > else: return path2 > else: return path1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Nemesis wrote: On my Win2000 box it returns "%USERPROFILE%". That's no surprise because if you look at the code it try to use os.environ["HOME"] (as os.path.expanduser() does). And on my Win2000 system this information points to "%USERPROFILE%" field (I don't know why, I'm not the administrator of that machine). It looks a lot like somebody who didn't test things out properly (the real administrator, not you), was trying to do the equivalent of "set home=%userprofile%" but managed to fail... if you do that at the command line or in a .CMD or .BAT file, it will expand the reference out and you'd have a "HOME=C:\Documents and Settings\whatever" just like you'd expect. Probably they put that in a dialog box somewhere without realizing it wouldn't be evaluated. (At least, I don't think environment variable substitution generally or ever has "lazy evaluation" semantics...) Now that I know how to do what Duncan described (thanks Duncan!), either approach works for me. Of course, whether I'm a likely eventual user of your particular program is an entirely different question. ;-) -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Mentre io pensavo ad una intro simpatica "Peter Hansen" scriveva: > Miki Tebeka wrote: >>>Hi all, I'm trying to write a multiplatform function that tries to >>>return the actual user home directory. >>>... >> >> What's wrong with: >> from user import home >> which does about what your code does. > > :-) > > I suspect he simply didn't know about it. I didn't either... That's true :-D But as I said in the other post os.environ["HOME"] doesn't work on my Win2000 box. > Nemesis, please use the above recipe instead, as it makes > the more reasonable (IMHO) choice of checking for a HOME > environment variable before trying the expanduser("~") > approach. This covers folks like me who, though stuck > using Windows, despise the ridiculous Microsoft convention > of "user folders" named like "C:\Documents and Settings\Peter" > and prefer to create sensible folder like c:\users\peter > and set a HOME variable to point to it. Your approach > ignores our HOME variable. If you look at ntpath.py (I think this is the 'path' module on Windows 2000) in you Lib dir you'll see that expanduser does try os.environ["HOME"] So I'm not ignoring it, maybe it is redundant in my function. The problem is that expanduser and user.home doesn't test if the value returned is really a directory. > c:\>python > Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 > >>> from user import home > >>> print home > c:\users\peter > > Yay! :-) on my box it returns "%USERPROFILE%" ;-) -- Forgive your enemies, but never forget their names. |\ | |HomePage : http://nem01.altervista.org | \|emesis |XPN (my nr): http://xpn.altervista.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Mentre io pensavo ad una intro simpatica "Miki Tebeka" scriveva: >> Hi all, I'm trying to write a multiplatform function that tries to >> return the actual user home directory. >> ... > What's wrong with: > from user import home > which does about what your code does. On my Win2000 box it returns "%USERPROFILE%". That's no surprise because if you look at the code it try to use os.environ["HOME"] (as os.path.expanduser() does). And on my Win2000 system this information points to "%USERPROFILE%" field (I don't know why, I'm not the administrator of that machine). -- Reality is for people who can't face science fiction. |\ | |HomePage : http://nem01.altervista.org | \|emesis |XPN (my nr): http://xpn.altervista.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
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 <[EMAIL PROTECTED]> http://kavlon.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Peter Hansen wrote: > Nemesis, please use the above recipe instead, as it makes > the more reasonable (IMHO) choice of checking for a HOME > environment variable before trying the expanduser("~") > approach. This covers folks like me who, though stuck > using Windows, despise the ridiculous Microsoft convention > of "user folders" named like "C:\Documents and Settings\Peter" > and prefer to create sensible folder like c:\users\peter > and set a HOME variable to point to it. Your approach > ignores our HOME variable. You could just tell Windows to move your home directory location to c:\users\peter and then all the environment variables will be set for you automatically. It probably varies wildly between Windows versions, but to assign a home folder for a local user on XP go to Control Panel/Administrative Tools/Computer Management/Local Users and Groups/Users, select your account, right menu Properties, Profile tab, and edit the 'Home folder' setting. Press F1 while editing the home folder location and see that at least someone at Microsoft thinks c:\users\... is a sensible location. See also http://support.microsoft.com/?kbid=816313 for domain users etc. -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Bernhard Herzog wrote: Peter Hansen <[EMAIL PROTECTED]> writes: Miki Tebeka wrote: Hi all, I'm trying to write a multiplatform function that tries to return the actual user home directory. ... What's wrong with: from user import home which does about what your code does. :-) I suspect he simply didn't know about it. I didn't either... The purpose of the user module is executing ~/.pythonrc.py, which may not desirable. It definitely shouldn't be done by a library, for instance. Also, that the user's home directory is available as user.home is not documented, and I for one wouldn't want to rely on that. Then please interpret my "please use the above recipe" as suggesting he should do what the equivalent code in the user.py module does in terms of finding the "home folder" of the user, rather than his own home-grown approach. -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Peter Hansen <[EMAIL PROTECTED]> writes: > Miki Tebeka wrote: >>>Hi all, I'm trying to write a multiplatform function that tries to >>>return the actual user home directory. >>>... >> What's wrong with: >> from user import home >> which does about what your code does. > > :-) > > I suspect he simply didn't know about it. I didn't either... The purpose of the user module is executing ~/.pythonrc.py, which may not desirable. It definitely shouldn't be done by a library, for instance. Also, that the user's home directory is available as user.home is not documented, and I for one wouldn't want to rely on that. Bernhard -- Intevation GmbH http://intevation.de/ Skencil http://skencil.org/ Thuban http://thuban.intevation.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Works great with Python 2.3.4 on... dare I say it... windows xp >>> getHomeDir() 'C:\\Documents and Settings\\Lars' Regards Lars Nemesis wrote: > (..) > Please, could you test it on your systems and tell me what you got? > (..) -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Miki Tebeka wrote: Hi all, I'm trying to write a multiplatform function that tries to return the actual user home directory. ... What's wrong with: from user import home which does about what your code does. :-) I suspect he simply didn't know about it. I didn't either... Nemesis, please use the above recipe instead, as it makes the more reasonable (IMHO) choice of checking for a HOME environment variable before trying the expanduser("~") approach. This covers folks like me who, though stuck using Windows, despise the ridiculous Microsoft convention of "user folders" named like "C:\Documents and Settings\Peter" and prefer to create sensible folder like c:\users\peter and set a HOME variable to point to it. Your approach ignores our HOME variable. c:\>python Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32 >>> from user import home >>> print home c:\users\peter Yay! :-) -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Miki Tebeka <[EMAIL PROTECTED]> wrote: > Hello Nemesis, > >> Hi all, I'm trying to write a multiplatform function that tries to >> return the actual user home directory. >> ... > What's wrong with: >from user import home > which does about what your code does. Except it also execfile()s $HOME/.pythonrc.py, which might not be wanted. Saluton Marc -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Hello Nemesis, > Hi all, I'm trying to write a multiplatform function that tries to > return the actual user home directory. > ... What's wrong with: from user import home which does about what your code does. Bye. -- Miki Tebeka <[EMAIL PROTECTED]> http://tebeka.bizhat.com The only difference between children and adults is the price of the toys -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
On Dé Céadaoin, Feabh 2, 2005, at 13:26 America/Chicago, Nemesis wrote: Hi all, I'm trying to write a multiplatform function that tries to return the actual user home directory. I saw that Please, could you test it on your systems and tell me what you got? I'd like to know what it returns on different operating systems because I'm developing a multiplatform software. It works with Python 2.2 and Python 2.3 on Mac OS 10.2 and Debian Linux. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Nemesis said the following on 2/2/2005 2:26 PM: Hi all, I'm trying to write a multiplatform function that tries to def getHomeDir(): ''' Try to find user's home directory, otherwise return current directory.''' Please, could you test it on your systems and tell me what you got? I'd like to know what it returns on different operating systems because I'm developing a multiplatform software. Thank you all. Neat! Works fine on Freebsd 5.3, Python 2.4 Thanks, -Kartic -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Python 2.4 (#1, Jan 1 2005, 21:33:55) [GCC 3.3.4] on linux2 Slackware 10 Current Works! Nice. -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
Nemesis wrote: Hi all, I'm trying to write a multiplatform function that tries to return the actual user home directory. I saw that os.path.expanduser("~") works on Linux but on Windows2000 (at least on the win I used) it returns %USERPROFILE%, so I tried os.environ["HOME"] and it gave me the same results. So I ended up with os.environ["USERPROFILE"], it doesn't work on Linux but (at least) on Windows2000 it returns the correct information I googled a little bit and it seems that there is no general solution, so I tried to merge what I found, and I wrote this little function: def getHomeDir(): ''' Try to find user's home directory, otherwise return current directory.''' try: path1=os.path.expanduser("~") except: path1="" try: path2=os.environ["HOME"] except: path2="" try: path3=os.environ["USERPROFILE"] except: path3="" if not os.path.exists(path1): if not os.path.exists(path2): if not os.path.exists(path3): return os.getcwd() else: return path3 else: return path2 else: return path1 Please, could you test it on your systems and tell me what you got? I'd like to know what it returns on different operating systems because I'm developing a multiplatform software. Thank you all. It works on Cygwin too! regards Steve -- Meet the Python developers and your c.l.py favorites March 23-25 Come to PyCon DC 2005 http://www.python.org/pycon/2005/ Steve Holden http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
On Wed, 02 Feb 2005 11:30:34 -0800 (PST), Nemesis <[EMAIL PROTECTED]> wrote: > Hi all, I'm trying to write a multiplatform function that tries to > return the actual user home directory. I saw that > os.path.expanduser("~") works on Linux but on Windows2000 (at least on > the win I used) it returns %USERPROFILE%, so I tried os.environ["HOME"] > and it gave me the same results. So I ended up with > os.environ["USERPROFILE"], it doesn't work on Linux but (at least) on > Windows2000 it returns the correct information > > I googled a little bit and it seems that there is no general solution, > so I tried to merge what I found, and I wrote this little function: > > def getHomeDir(): > ''' Try to find user's home directory, otherwise return current > directory.''' > try: > path1=os.path.expanduser("~") > except: > path1="" > try: > path2=os.environ["HOME"] > except: > path2="" > try: > path3=os.environ["USERPROFILE"] > except: > path3="" > > if not os.path.exists(path1): > if not os.path.exists(path2): > if not os.path.exists(path3): > return os.getcwd() > else: return path3 > else: return path2 > else: return path1 > > Please, could you test it on your systems and tell me what you got? > I'd like to know what it returns on different operating systems because > I'm developing a multiplatform software. > > Thank you all. > -- > Unauthorized amphibians will be toad away. > > |\ | |HomePage : http://nem01.altervista.org > | \|emesis |XPN (my nr): http://xpn.altervista.org > > -- > http://mail.python.org/mailman/listinfo/python-list > Works beautifully on my PowerBook running Mac OSX 10.3.7 /Users/timothygrant -- Stand Fast, tjg. -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
on win xp home, python 2.4 its also correct for me -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
On Wed, 02 Feb 2005 19:26:00 +, Nemesis wrote: > Please, could you test it on your systems and tell me what you got? > I'd like to know what it returns on different operating systems because > I'm developing a multiplatform software. > > Thank you all. I tried your function in my environment (Python 2.3.3 on Linux) and it returned the home directory properly - - Mark Nenadov Python Byte Solutions http://www.pythonbyte.com/ -- http://mail.python.org/mailman/listinfo/python-list
Finding user's home dir
Hi all, I'm trying to write a multiplatform function that tries to return the actual user home directory. I saw that os.path.expanduser("~") works on Linux but on Windows2000 (at least on the win I used) it returns %USERPROFILE%, so I tried os.environ["HOME"] and it gave me the same results. So I ended up with os.environ["USERPROFILE"], it doesn't work on Linux but (at least) on Windows2000 it returns the correct information I googled a little bit and it seems that there is no general solution, so I tried to merge what I found, and I wrote this little function: def getHomeDir(): ''' Try to find user's home directory, otherwise return current directory.''' try: path1=os.path.expanduser("~") except: path1="" try: path2=os.environ["HOME"] except: path2="" try: path3=os.environ["USERPROFILE"] except: path3="" if not os.path.exists(path1): if not os.path.exists(path2): if not os.path.exists(path3): return os.getcwd() else: return path3 else: return path2 else: return path1 Please, could you test it on your systems and tell me what you got? I'd like to know what it returns on different operating systems because I'm developing a multiplatform software. Thank you all. -- Unauthorized amphibians will be toad away. |\ | |HomePage : http://nem01.altervista.org | \|emesis |XPN (my nr): http://xpn.altervista.org -- http://mail.python.org/mailman/listinfo/python-list