Finding the "home" folder [Linux/Windows]

Trent Mick trentm at ActiveState.com
Sat Nov 1 17:40:36 EST 2003


[Luca T. wrote]
> > i need a way to find the home folder of the current user no matter if i am
> > in Linux or Windows for instance:
> >
> > Is there any general way to do this?

On Linux you can just get the HOME environment variable value or just
use:
    os.path.expanduser("~")

On Windows, as Mike said, "you're supposed to store difference types of
things in different directories". Here is a description of those
difference directories as MS defines them:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/enums/csidl.asp

The CSIDL_* flags are values that meant to passed to the Win32 API call:

    SHGetFolderPath()

If you have PyWin32 (aka win32all) installed in your Python installation
then you can do this:

    from win32com.shell import shellcon, shell
    path = shell.SHGetFolderPath(0, shellcon.CSIDL_*, 0, 0)

Cheers,
Trent


-- 
Trent Mick
TrentM at ActiveState.com





More information about the Python-list mailing list