How to get the My Documents path in all versions of Windows
If you’re looking for a generic way to find the variable folders such as a user’s “My Documents” folder in windows, os.environ variables won’t always work because in Windows they change from one version to another. However, the following method works in Windows XP and Windows 7, which is what I needed:
12345 import ctypesdll = ctypes.windll.shell32buf = ctypes.create_string_buffer(300)dll.SHGetSpecialFolderPathA(None, buf, 0x0005, False)print buf.value
My first attempt was to use os.environ, but the variable that led me to the app data directory I was looking for was different between XP and 7.
worked on Windows 7, but in Windows XP this is what I needed instead:
That’s too bad too, because this is oh-so much easier on the eyes than ctypes.