| 1 |
"""Utilities for retrieving known Windows folder paths using the Windows Shell API. |
| 2 |
|
| 3 |
It defines constants for various known folder GUIDs and provides a function to |
| 4 |
retrieve their paths for the current or common user. The implementation uses |
| 5 |
the `ctypes` library for interacting with the Windows API. |
| 6 |
""" |
| 7 |
# knownpaths.py (https://gist.github.com/mkropat/7550097) |
| 8 |
# |
| 9 |
# The MIT License (MIT) |
| 10 |
# |
| 11 |
# Copyright (c) 2014 Michael Kropat |
| 12 |
# |
| 13 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 |
# of this software and associated documentation files (the "Software"), to deal |
| 15 |
# in the Software without restriction, including without limitation the rights |
| 16 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 |
# copies of the Software, and to permit persons to whom the Software is |
| 18 |
# furnished to do so, subject to the following conditions: |
| 19 |
# |
| 20 |
# The above copyright notice and this permission notice shall be included in |
| 21 |
# all copies or substantial portions of the Software. |
| 22 |
# |
| 23 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 29 |
# THE SOFTWARE. |
| 30 |
# |
| 31 |
# I explicitly permit the knownpaths.py source code to be licensed by anyone |
| 32 |
# under the terms of the GNU GPL v3, as an alternative to the X11/MIT license. |
| 33 |
|
| 34 |
from __future__ import annotations |
| 35 |
|
| 36 |
import ctypes |
| 37 |
import sys |
| 38 |
from ctypes import windll, wintypes |
| 39 |
from typing import ClassVar |
| 40 |
from uuid import UUID |
| 41 |
|
| 42 |
|
| 43 |
class GUID(ctypes.Structure): # [1] |
| 44 |
"""Class representing a GUID (Globally Unique Identifier).""" |
| 45 |
|
| 46 |
_fields_: ClassVar[list[tuple]] = [ |
| 47 |
("Data1", wintypes.DWORD), |
| 48 |
("Data2", wintypes.WORD), |
| 49 |
("Data3", wintypes.WORD), |
| 50 |
("Data4", wintypes.BYTE * 8), |
| 51 |
] |
| 52 |
|
| 53 |
def __init__(self, uuid_): |
| 54 |
ctypes.Structure.__init__(self) |
| 55 |
( |
| 56 |
self.Data1, |
| 57 |
self.Data2, |
| 58 |
self.Data3, |
| 59 |
self.Data4[0], |
| 60 |
self.Data4[1], |
| 61 |
rest, |
| 62 |
) = uuid_.fields |
| 63 |
for i in range(2, 8): |
| 64 |
self.Data4[i] = rest >> (8 - i - 1) * 8 & 0xFF |
| 65 |
|
| 66 |
|
| 67 |
class FOLDERID: # [2] |
| 68 |
"""Class representing known folder GUIDs.""" |
| 69 |
|
| 70 |
AccountPictures = UUID("{008ca0b1-55b4-4c56-b8a8-4de4b299d3be}") |
| 71 |
AdminTools = UUID("{724EF170-A42D-4FEF-9F26-B60E846FBA4F}") |
| 72 |
ApplicationShortcuts = UUID("{A3918781-E5F2-4890-B3D9-A7E54332328C}") |
| 73 |
CameraRoll = UUID("{AB5FB87B-7CE2-4F83-915D-550846C9537B}") |
| 74 |
CDBurning = UUID("{9E52AB10-F80D-49DF-ACB8-4330F5687855}") |
| 75 |
CommonAdminTools = UUID("{D0384E7D-BAC3-4797-8F14-CBA229B392B5}") |
| 76 |
CommonOEMLinks = UUID("{C1BAE2D0-10DF-4334-BEDD-7AA20B227A9D}") |
| 77 |
CommonPrograms = UUID("{0139D44E-6AFE-49F2-8690-3DAFCAE6FFB8}") |
| 78 |
CommonStartMenu = UUID("{A4115719-D62E-491D-AA7C-E74B8BE3B067}") |
| 79 |
CommonStartup = UUID("{82A5EA35-D9CD-47C5-9629-E15D2F714E6E}") |
| 80 |
CommonTemplates = UUID("{B94237E7-57AC-4347-9151-B08C6C32D1F7}") |
| 81 |
Contacts = UUID("{56784854-C6CB-462b-8169-88E350ACB882}") |
| 82 |
Cookies = UUID("{2B0F765D-C0E9-4171-908E-08A611B84FF6}") |
| 83 |
Desktop = UUID("{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}") |
| 84 |
DeviceMetadataStore = UUID("{5CE4A5E9-E4EB-479D-B89F-130C02886155}") |
| 85 |
Documents = UUID("{FDD39AD0-238F-46AF-ADB4-6C85480369C7}") |
| 86 |
DocumentsLibrary = UUID("{7B0DB17D-9CD2-4A93-9733-46CC89022E7C}") |
| 87 |
Downloads = UUID("{374DE290-123F-4565-9164-39C4925E467B}") |
| 88 |
Favorites = UUID("{1777F761-68AD-4D8A-87BD-30B759FA33DD}") |
| 89 |
Fonts = UUID("{FD228CB7-AE11-4AE3-864C-16F3910AB8FE}") |
| 90 |
GameTasks = UUID("{054FAE61-4DD8-4787-80B6-090220C4B700}") |
| 91 |
History = UUID("{D9DC8A3B-B784-432E-A781-5A1130A75963}") |
| 92 |
ImplicitAppShortcuts = UUID("{BCB5256F-79F6-4CEE-B725-DC34E402FD46}") |
| 93 |
InternetCache = UUID("{352481E8-33BE-4251-BA85-6007CAEDCF9D}") |
| 94 |
Libraries = UUID("{1B3EA5DC-B587-4786-B4EF-BD1DC332AEAE}") |
| 95 |
Links = UUID("{bfb9d5e0-c6a9-404c-b2b2-ae6db6af4968}") |
| 96 |
LocalAppData = UUID("{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}") |
| 97 |
LocalAppDataLow = UUID("{A520A1A4-1780-4FF6-BD18-167343C5AF16}") |
| 98 |
LocalizedResourcesDir = UUID("{2A00375E-224C-49DE-B8D1-440DF7EF3DDC}") |
| 99 |
Music = UUID("{4BD8D571-6D19-48D3-BE97-422220080E43}") |
| 100 |
MusicLibrary = UUID("{2112AB0A-C86A-4FFE-A368-0DE96E47012E}") |
| 101 |
NetHood = UUID("{C5ABBF53-E17F-4121-8900-86626FC2C973}") |
| 102 |
OriginalImages = UUID("{2C36C0AA-5812-4b87-BFD0-4CD0DFB19B39}") |
| 103 |
PhotoAlbums = UUID("{69D2CF90-FC33-4FB7-9A0C-EBB0F0FCB43C}") |
| 104 |
PicturesLibrary = UUID("{A990AE9F-A03B-4E80-94BC-9912D7504104}") |
| 105 |
Pictures = UUID("{33E28130-4E1E-4676-835A-98395C3BC3BB}") |
| 106 |
Playlists = UUID("{DE92C1C7-837F-4F69-A3BB-86E631204A23}") |
| 107 |
PrintHood = UUID("{9274BD8D-CFD1-41C3-B35E-B13F55A758F4}") |
| 108 |
Profile = UUID("{5E6C858F-0E22-4760-9AFE-EA3317B67173}") |
| 109 |
ProgramData = UUID("{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}") |
| 110 |
ProgramFiles = UUID("{905e63b6-c1bf-494e-b29c-65b732d3d21a}") |
| 111 |
ProgramFilesX64 = UUID("{6D809377-6AF0-444b-8957-A3773F02200E}") |
| 112 |
ProgramFilesX86 = UUID("{7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}") |
| 113 |
ProgramFilesCommon = UUID("{F7F1ED05-9F6D-47A2-AAAE-29D317C6F066}") |
| 114 |
ProgramFilesCommonX64 = UUID("{6365D5A7-0F0D-45E5-87F6-0DA56B6A4F7D}") |
| 115 |
ProgramFilesCommonX86 = UUID("{DE974D24-D9C6-4D3E-BF91-F4455120B917}") |
| 116 |
Programs = UUID("{A77F5D77-2E2B-44C3-A6A2-ABA601054A51}") |
| 117 |
Public = UUID("{DFDF76A2-C82A-4D63-906A-5644AC457385}") |
| 118 |
PublicDesktop = UUID("{C4AA340D-F20F-4863-AFEF-F87EF2E6BA25}") |
| 119 |
PublicDocuments = UUID("{ED4824AF-DCE4-45A8-81E2-FC7965083634}") |
| 120 |
PublicDownloads = UUID("{3D644C9B-1FB8-4f30-9B45-F670235F79C0}") |
| 121 |
PublicGameTasks = UUID("{DEBF2536-E1A8-4c59-B6A2-414586476AEA}") |
| 122 |
PublicLibraries = UUID("{48DAF80B-E6CF-4F4E-B800-0E69D84EE384}") |
| 123 |
PublicMusic = UUID("{3214FAB5-9757-4298-BB61-92A9DEAA44FF}") |
| 124 |
PublicPictures = UUID("{B6EBFB86-6907-413C-9AF7-4FC2ABF07CC5}") |
| 125 |
PublicRingtones = UUID("{E555AB60-153B-4D17-9F04-A5FE99FC15EC}") |
| 126 |
PublicUserTiles = UUID("{0482af6c-08f1-4c34-8c90-e17ec98b1e17}") |
| 127 |
PublicVideos = UUID("{2400183A-6185-49FB-A2D8-4A392A602BA3}") |
| 128 |
QuickLaunch = UUID("{52a4f021-7b75-48a9-9f6b-4b87a210bc8f}") |
| 129 |
Recent = UUID("{AE50C081-EBD2-438A-8655-8A092E34987A}") |
| 130 |
RecordedTVLibrary = UUID("{1A6FDBA2-F42D-4358-A798-B74D745926C5}") |
| 131 |
ResourceDir = UUID("{8AD10C31-2ADB-4296-A8F7-E4701232C972}") |
| 132 |
Ringtones = UUID("{C870044B-F49E-4126-A9C3-B52A1FF411E8}") |
| 133 |
RoamingAppData = UUID("{3EB685DB-65F9-4CF6-A03A-E3EF65729F3D}") |
| 134 |
RoamedTileImages = UUID("{AAA8D5A5-F1D6-4259-BAA8-78E7EF60835E}") |
| 135 |
RoamingTiles = UUID("{00BCFC5A-ED94-4e48-96A1-3F6217F21990}") |
| 136 |
SampleMusic = UUID("{B250C668-F57D-4EE1-A63C-290EE7D1AA1F}") |
| 137 |
SamplePictures = UUID("{C4900540-2379-4C75-844B-64E6FAF8716B}") |
| 138 |
SamplePlaylists = UUID("{15CA69B3-30EE-49C1-ACE1-6B5EC372AFB5}") |
| 139 |
SampleVideos = UUID("{859EAD94-2E85-48AD-A71A-0969CB56A6CD}") |
| 140 |
SavedGames = UUID("{4C5C32FF-BB9D-43b0-B5B4-2D72E54EAAA4}") |
| 141 |
SavedSearches = UUID("{7d1d3a04-debb-4115-95cf-2f29da2920da}") |
| 142 |
Screenshots = UUID("{b7bede81-df94-4682-a7d8-57a52620b86f}") |
| 143 |
SearchHistory = UUID("{0D4C3DB6-03A3-462F-A0E6-08924C41B5D4}") |
| 144 |
SearchTemplates = UUID("{7E636BFE-DFA9-4D5E-B456-D7B39851D8A9}") |
| 145 |
SendTo = UUID("{8983036C-27C0-404B-8F08-102D10DCFD74}") |
| 146 |
SidebarDefaultParts = UUID("{7B396E54-9EC5-4300-BE0A-2482EBAE1A26}") |
| 147 |
SidebarParts = UUID("{A75D362E-50FC-4fb7-AC2C-A8BEAA314493}") |
| 148 |
SkyDrive = UUID("{A52BBA46-E9E1-435f-B3D9-28DAA648C0F6}") |
| 149 |
SkyDriveCameraRoll = UUID("{767E6811-49CB-4273-87C2-20F355E1085B}") |
| 150 |
SkyDriveDocuments = UUID("{24D89E24-2F19-4534-9DDE-6A6671FBB8FE}") |
| 151 |
SkyDrivePictures = UUID("{339719B5-8C47-4894-94C2-D8F77ADD44A6}") |
| 152 |
StartMenu = UUID("{625B53C3-AB48-4EC1-BA1F-A1EF4146FC19}") |
| 153 |
Startup = UUID("{B97D20BB-F46A-4C97-BA10-5E3608430854}") |
| 154 |
System = UUID("{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}") |
| 155 |
SystemX86 = UUID("{D65231B0-B2F1-4857-A4CE-A8E7C6EA7D27}") |
| 156 |
Templates = UUID("{A63293E8-664E-48DB-A079-DF759E0509F7}") |
| 157 |
UserPinned = UUID("{9E3995AB-1F9C-4F13-B827-48B24B6C7174}") |
| 158 |
UserProfiles = UUID("{0762D272-C50A-4BB0-A382-697DCD729B80}") |
| 159 |
UserProgramFiles = UUID("{5CD7AEE2-2219-4A67-B85D-6C9CE15660CB}") |
| 160 |
UserProgramFilesCommon = UUID("{BCBD3057-CA5C-4622-B42D-BC56DB0AE516}") |
| 161 |
Videos = UUID("{18989B1D-99B5-455B-841C-AB7C74E4DDFC}") |
| 162 |
VideosLibrary = UUID("{491E922F-5643-4AF4-A7EB-4E7A138D8174}") |
| 163 |
Windows = UUID("{F38BF404-1D43-42F2-9305-67DE0B28FC23}") |
| 164 |
|
| 165 |
|
| 166 |
class UserHandle: # [3] |
| 167 |
"""Class representing user handles for accessing known folder paths.""" |
| 168 |
|
| 169 |
current = wintypes.HANDLE(0) |
| 170 |
common = wintypes.HANDLE(-1) |
| 171 |
|
| 172 |
|
| 173 |
_CoTaskMemFree = windll.ole32.CoTaskMemFree # [4] |
| 174 |
_CoTaskMemFree.restype = None |
| 175 |
_CoTaskMemFree.argtypes = [ctypes.c_void_p] |
| 176 |
|
| 177 |
_SHGetKnownFolderPath = windll.shell32.SHGetKnownFolderPath # [5] [3] |
| 178 |
_SHGetKnownFolderPath.argtypes = [ |
| 179 |
ctypes.POINTER(GUID), |
| 180 |
wintypes.DWORD, |
| 181 |
wintypes.HANDLE, |
| 182 |
ctypes.POINTER(ctypes.c_wchar_p), |
| 183 |
] |
| 184 |
|
| 185 |
|
| 186 |
class PathNotFoundError(Exception): |
| 187 |
"""Exception raised when a known folder path is not found.""" |
| 188 |
|
| 189 |
|
| 190 |
def get_path(folder_id, user_handle=UserHandle.common): |
| 191 |
"""Get the path of a known folder by its GUID. |
| 192 |
|
| 193 |
Args: |
| 194 |
folder_id (UUID): The GUID of the known folder. |
| 195 |
user_handle (wintypes.HANDLE): The user handle to use (current or common). |
| 196 |
|
| 197 |
Raises: |
| 198 |
PathNotFoundError: If the known folder path cannot be found. |
| 199 |
|
| 200 |
Returns: |
| 201 |
str: The path of the known folder. |
| 202 |
""" |
| 203 |
fid = GUID(folder_id) |
| 204 |
pPath = ctypes.c_wchar_p() |
| 205 |
S_OK = 0 |
| 206 |
if ( |
| 207 |
_SHGetKnownFolderPath(ctypes.byref(fid), 0, user_handle, ctypes.byref(pPath)) |
| 208 |
!= S_OK |
| 209 |
): |
| 210 |
raise PathNotFoundError |
| 211 |
path = pPath.value |
| 212 |
_CoTaskMemFree(pPath) |
| 213 |
return path |
| 214 |
|
| 215 |
|
| 216 |
if __name__ == "__main__": |
| 217 |
if len(sys.argv) < 2 or sys.argv[1] in ["-?", "/?"]: |
| 218 |
print("python knownpaths.py FOLDERID {current|common}") |
| 219 |
sys.exit(0) |
| 220 |
|
| 221 |
try: |
| 222 |
folder_id = getattr(FOLDERID, sys.argv[1]) |
| 223 |
except AttributeError: |
| 224 |
print(f'Unknown folder id "{sys.argv[1]}"', file=sys.stderr) |
| 225 |
sys.exit(1) |
| 226 |
|
| 227 |
try: |
| 228 |
if len(sys.argv) == 2: |
| 229 |
print(get_path(folder_id)) |
| 230 |
else: |
| 231 |
print(get_path(folder_id, getattr(UserHandle, sys.argv[2]))) |
| 232 |
except PathNotFoundError: |
| 233 |
print('Folder not found "{}"'.format(" ".join(sys.argv[1:])), file=sys.stderr) |
| 234 |
sys.exit(1) |
| 235 |
|
| 236 |
# [1] http://msdn.microsoft.com/en-us/library/windows/desktop/aa373931.aspx |
| 237 |
# [2] http://msdn.microsoft.com/en-us/library/windows/desktop/dd378457.aspx |
| 238 |
# [3] http://msdn.microsoft.com/en-us/library/windows/desktop/bb762188.aspx |
| 239 |
# [4] http://msdn.microsoft.com/en-us/library/windows/desktop/ms680722.aspx |
| 240 |
# [5] http://web.archive.org/web/20111025090317/http://www.themacaque.com/?p=954 |