I learned the code for a function that opens a special Windows folder using ctypes in Python 3 and the process of writing it.
Open font folder
import os
import ctypes
from ctypes import oledll, wintypes
def getshellfolderpath(csidl: int) -> str:
if not isinstance(csidl, int):
raise TypeError
SHGetFolderPathW = oledll.shell32.SHGetFolderPathW
SHGetFolderPathW.argtypes = [
wintypes.HWND, ctypes.c_int, wintypes.HANDLE,
wintypes.DWORD, wintypes.LPWSTR]
path = ctypes.create_unicode_buffer(wintypes.MAX_PATH)
SHGetFolderPathW(0, csidl, 0, 0, path)
return path.value
CSIDL_FONTS = 0x0014
os.startfile(getshellfolderpath(CSIDL_FONTS))
--In Python 3, you can use the standard library ctypes to work with the Win32 API.
--General types of Win32 API (HWND
, DWORD
, etc.) are defined in ctypes.wintypes
.
--ctypes
exports cdll
, pydll
, windll
, ʻoledllfor loading the dynamically linked library.
cdll and
pydllare the cdecl calling convention, and
windll and
ctypes.oledllare the stdcall calling convention.
ctypes.oledll assumes a return value of
HRESULT and throws ʻOSError
on failure. Official documentation.
--Create a WCHAR
type buffer with the ctypes.create_unicode_buffer
function. You can get the string with the return value value
. Official documentation.
--You can use the ʻos.startfile` function to open files / folders by association.
Recommended Posts