OS independent retrieval of home directory

Useful hints
Post Reply
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

OS independent retrieval of home directory

Post by hproe »

Good people -

In scripts it is useful to refer I/O operations to the home directory. Buried somewhere in the net I have found the following elegant way to do this independent of the OS:
homeDir = os.path.expanduser('~')

It works under Windows and Linux, and on Macs.

HP
User avatar
Jon
Posts: 192
Joined: Fri Jan 09, 2009 8:44 pm
Location: Madison, WI

Re: OS independent retrieval of home directory

Post by Jon »

There's a function called "_expandpath" in interactive.py that basically pipes a path string through expand[user|vars|whatnot] and so basically you can pass in something like:

Code: Select all

_expandpath('/tmp/test/../$USER/./filename.ext')


and get something like '/tmp/jbeavers/filename.ext' returned.
Post Reply