What is the best method to determine the path of Windows special-folders and known-folders in python?
I've uncovered a couple of popular methods for SpecialFolders, a.k.a CSIDL, but nothing straightforward yet for KNOWNFOLDERID. Backwards compatibility is maintained, so the CSIDL methods still work, but any special/known folders introduced since Windows XP are not enumerated there. Examples of the "new" unavailable ones include Downloads, Playlists, Program Files x64.
Special Folders
Find system folder locations in Python - simplest CSIDL method, relies on win32com, good for AllUsersDesktop, AllUsersStartMenu, AllUsersPrograms, AllUsersStartup, Desktop, Favorites, Fonts, MyDocuments, NetHood, PrintHood, Recent, SendTo, StartMenu, Startup & Templates (list probably incomplete).
How to get path of Start Menu's Programs directory? - has same approach as above, as well as method using ctypes directly, so win32com not needed (not as clear or straightforward)
Known Folders
- ?
Both in one
- ?
You solve this problem the same way as that one: First you find the documentation and ideally some nice sample code in C++, C#, or VB, then you figure out how to use PyWin32 to make the same shell API or
IKnownFolder
COM calls from Python.As the MSDN overview documentation on Known Folders says, you can use the new shell wrapper function
SHGetKnownFolderPath
instead of the oldSHFolderPath
orSHGetFolderPath
, or you can use the completeIKnownFolderManager
interface via COM.Unfortunately, I don't have a Windows machine in front of me, and MSDN's sample downloads are not responding, so I'm going to have to do a bit of guessing. But it may be something like this:
If
shellcon
doesn't have theFOLDERID
values, you'll have to look them up on theKNOWNFOLDERID
and define the constants you need yourself.If
shell
doesn't have theSHGetKnownFolderPath
function, you'll have to instantiate anIKnownFolderManager
and callGetFolderByName
.If
shell
doesn't even haveIKnownFolderManager
… but a quick Google shows it was added in build 218, so that's not going to be an issue.If you'd rather do it via
ctypes
thanwin32com
, it would look something like this (again, untested because I don't have a Windows box and MSDN's server is broken):