Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 18th, 2005, 10:00 PM
Nemesis
Guest
 
Posts: n/a
Default 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

  #2  
Old July 18th, 2005, 10:00 PM
Mark Nenadov
Guest
 
Posts: n/a
Default Re: Finding user's home dir

On Wed, 02 Feb 2005 19:26:00 +0000, Nemesis wrote:
[color=blue]
> 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.[/color]

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/
  #3  
Old July 18th, 2005, 10:00 PM
wittempj@hotmail.com
Guest
 
Posts: n/a
Default Re: Finding user's home dir

on win xp home, python 2.4 its also correct for me

  #4  
Old July 18th, 2005, 10:00 PM
Timothy Grant
Guest
 
Posts: n/a
Default Re: Finding user's home dir

On Wed, 02 Feb 2005 11:30:34 -0800 (PST), Nemesis
<nemesis@nowhere.invalid> wrote:[color=blue]
> 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
>[/color]

Works beautifully on my PowerBook running Mac OSX 10.3.7

/Users/timothygrant

--
Stand Fast,
tjg.
  #5  
Old July 18th, 2005, 10:00 PM
Steve Holden
Guest
 
Posts: n/a
Default Re: Finding user's home dir

Nemesis wrote:
[color=blue]
> 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.[/color]

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/
  #6  
Old July 18th, 2005, 10:01 PM
Nick
Guest
 
Posts: n/a
Default 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.
  #7  
Old July 18th, 2005, 10:01 PM
Kartic
Guest
 
Posts: n/a
Default Re: Finding user's home dir

Nemesis said the following on 2/2/2005 2:26 PM:[color=blue]
> 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.[/color]


Neat!

Works fine on Freebsd 5.3, Python 2.4

Thanks,
-Kartic
  #8  
Old July 18th, 2005, 10:01 PM
Miki Tebeka
Guest
 
Posts: n/a
Default Re: Finding user's home dir

Hello Nemesis,
[color=blue]
> Hi all, I'm trying to write a multiplatform function that tries to
> return the actual user home directory.
> ...[/color]
What's wrong with:
from user import home
which does about what your code does.

Bye.
--
------------------------------------------------------------------------
Miki Tebeka <miki.tebeka@zoran.com>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys
  #9  
Old July 18th, 2005, 10:01 PM
Marc Christiansen
Guest
 
Posts: n/a
Default Re: Finding user's home dir

Miki Tebeka <miki.tebeka@zoran.com> wrote:[color=blue]
> Hello Nemesis,
>[color=green]
>> Hi all, I'm trying to write a multiplatform function that tries to
>> return the actual user home directory.
>> ...[/color]
> What's wrong with:
> from user import home
> which does about what your code does.[/color]

Except it also execfile()s $HOME/.pythonrc.py, which might not be wanted.

Saluton
Marc
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles