この問題は、その問題と同じ方法で解決します。まず、ドキュメントと、できれば C++、C#、または VB の優れたサンプル コードを見つけてから、PyWin32 を使用してIKnownFolder
Python から同じシェル API または COM 呼び出しを行う方法を見つけます。
既知のフォルダーに関する MSDN の概要ドキュメントにあるように、古いまたはSHGetKnownFolderPath
の代わりに新しいシェル ラッパー関数を使用するか、 COM 経由で完全なインターフェイスを使用できます。SHFolderPath
SHGetFolderPath
IKnownFolderManager
残念ながら、目の前に Windows マシンがなく、MSDN のサンプル ダウンロードが応答しないため、少し推測する必要があります。しかし、それは次のようなものかもしれません:
from win32com.shell import shell, shellcon
path = shell.SHGetKnownFolderPath(shellcon.FOLDERID_AccountPictures,
0, # see KNOWN_FOLDER_FLAG
0) # current user
shellcon
に値がない場合はFOLDERID
、 でそれらを調べて、KNOWNFOLDERID
必要な定数を自分で定義する必要があります。
に関数shell
がない場合は、 をインスタンス化してを呼び出す必要があります。SHGetKnownFolderPath
IKnownFolderManager
GetFolderByName
shell
さえないなら…IKnownFolderManager
しかし、簡単なGoogleはそれがビルド218で追加されたことを示しているので、それは問題にはなりません。
ctypes
よりも経由で実行したい場合はwin32com
、次のようになります (これも、Windows ボックスがなく、MSDN のサーバーが壊れているためテストされていません)。
from ctypes import windll, wintypes
from ctypes import *
from uuid import UUID
# ctypes GUID copied from MSDN sample code
class GUID(Structure):
_fields_ = [
("Data1", wintypes.DWORD),
("Data2", wintypes.WORD),
("Data3", wintypes.WORD),
("Data4", wintypes.BYTE * 8)
]
def __init__(self, uuidstr):
uuid = UUID(uuidstr)
Structure.__init__(self)
self.Data1, self.Data2, self.Data3, self.Data4[0], self.Data4[1], rest = uuid.fields
for i in range(2, 8):
self.Data4[i] = rest>>(8-i-1)*8 & 0xff
FOLDERID_AccountPictures = '{008ca0b1-55b4-4c56-b8a8-4de4b299d3be}'
SHGetKnownFolderPath = windll.shell32.SHGetKnownFolderPath
SHGetKnownFolderPath.argtypes = [
POINTER(GUID), wintypes.DWORD, wintypes.HANDLE, POINTER(c_char_p)]
def get_known_folder_path(uuidstr):
pathptr = c_wchar_p()
guid = GUID(uuidstr)
if SHGetKnownFolderPath(byref(guid), 0, 0, byref(pathptr)):
raise Exception('Whatever you want here...')
return pathptr.value