How to get Windows' special folders for currently logged-in user?

python, pywin32, windows

Solution

You can do it with the pywin32 extensions:

from win32com.shell import shell, shellcon
print shell.SHGetFolderPath(0, shellcon.CSIDL_MYPICTURES, None, 0)
# prints something like C:\Documents and Settings\Username\My Documents\My Pictures
# (Unicode object)

Check `shellcon.CSIDL_xxx` for other possible folders.

I think using pywin32 is the best way. Else you'd have to use `ctypes` to access the `SHGetFolderPath` function somehow (other solutions might be possible but these are the ones I know).

Problem

How can I get Windows special folders like `My Documents`, `Desktop`, etc. from my Python script? Do I need win32 extensions? It must work on Windows 2000 to Windows 7.

Original source