473,396 Members | 2,158 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,396 software developers and data experts.

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

Jul 18 '05 #1
8 2641
On Wed, 02 Feb 2005 19:26:00 +0000, 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/
Jul 18 '05 #2
on win xp home, python 2.4 its also correct for me

Jul 18 '05 #3
On Wed, 02 Feb 2005 11:30:34 -0800 (PST), Nemesis
<ne*****@nowhere.invalid> 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.
Jul 18 '05 #4
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/
Jul 18 '05 #5
Python 2.4 (#1, Jan 1 2005, 21:33:55)
[GCC 3.3.4] on linux2
Slackware 10 Current

Works! Nice.
Jul 18 '05 #6
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
Jul 18 '05 #7
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 <mi*********@zoran.com>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys
Jul 18 '05 #8
Miki Tebeka <mi*********@zoran.com> 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
Jul 18 '05 #9

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

Similar topics

4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
1
by: Todd Johnson | last post by:
Hey, I have been working on a program that stores information for each user in a subdirectory of the directory in which the script is run. So if my script is in /home/someuser/myprogram, the...
4
by: Luca T. | last post by:
Hello, i need a way to find the home folder of the current user no matter if i am in Linux or Windows for instance: * Linux: /home/username * Windows: C:\Documents and Settings\username Is...
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 +...
12
by: jeff elkins | last post by:
I'm creating an app that relies on a configuration file at launch. The file will always exist in the app's installation directory, but I have no control over where that might be. Is there an...
22
by: Tony Houghton | last post by:
I'm using pygame to write a game called Bombz which needs to save some data in a directory associated with it. In Unix/Linux I'd probably use "~/.bombz", in Windows something like "C:\Documents...
7
by: Jason Reichenbach | last post by:
I've GOT to be missing something painfully obvious, here... I need to programmatically get the full human name of the current user on a local system, the same system upon which the app is...
1
by: me | last post by:
Hi, Im having a few issues with finding exactly which program may be accessing a certain file. Lets say I need to replace a DLL with a newer version, but I need to make sure its not being...
5
by: ron.longo | last post by:
Is there any way that I can find the path of the main .py file of my application? For example, I have an application with some resources which are in a subdirectory: myPythonApp.py...
5
by: mohi | last post by:
hello everyone i m positing this again but can't help as im not finding any solution to this . my problem is i have to browse a directory to search for all the files in it and process certain...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.