Trouble getting to windows My Documents directory

random832 at random832 at
Sat Jul 11 02:24:04 CEST 2015

The My Documents directory is not guaranteed to be named "Documents". On
older versions of windows it was "My Documents", and on foreign versions
of windows it is a name in their language.

The correct way to get the path of this folder is, for example (couldn't
test since I'm on a mac right now)

import ctypes
from ctypes import wintypes


SHGetFolderPath = ctypes.windll.shell32.SHGetFolderPathW
pszPath = ctypes.create_unicode_buffer(wintypes.MAX_PATH)
SHGetFolderPath.argtypes = [wintypes.HWND, ctypes.c_int,
wintypes.HANDLE, wintypes.DWORD, LPWSTR]
SHGetFolderPath.restype = wintypes.HRESULT

hResult = SHGetFolderPath(0, CSIDL_PERSONAL, 0, 0, pszPath)

if hResult == 0:
  print pszPath.value
  raise OSError("Could not find My Documents directory")

Linux systems generally provide their own way to get it (e.g.
xdg-user-dirs), on OSX everything I can find indicates it really is
always ~/Documents .

More information about the Python-list mailing list