Show explorer properties dialog for file in Windows

Is there an easy way to show the properties dialog for a file in Windows using Python?

I am trying to show the same window that appears when you right-click a file in Explorer and select "Properties".

+7
source share
1 answer

The way to do this is to call the ShellExecuteEx() API, passing the verb properties . There are various high-level Python shells, but I have not been able to get any of them to work with the verb properties . Instead, I would use the good old ctypes .

 import time import ctypes import ctypes.wintypes SEE_MASK_NOCLOSEPROCESS = 0x00000040 SEE_MASK_INVOKEIDLIST = 0x0000000C class SHELLEXECUTEINFO(ctypes.Structure): _fields_ = ( ("cbSize",ctypes.wintypes.DWORD), ("fMask",ctypes.c_ulong), ("hwnd",ctypes.wintypes.HANDLE), ("lpVerb",ctypes.c_char_p), ("lpFile",ctypes.c_char_p), ("lpParameters",ctypes.c_char_p), ("lpDirectory",ctypes.c_char_p), ("nShow",ctypes.c_int), ("hInstApp",ctypes.wintypes.HINSTANCE), ("lpIDList",ctypes.c_void_p), ("lpClass",ctypes.c_char_p), ("hKeyClass",ctypes.wintypes.HKEY), ("dwHotKey",ctypes.wintypes.DWORD), ("hIconOrMonitor",ctypes.wintypes.HANDLE), ("hProcess",ctypes.wintypes.HANDLE), ) ShellExecuteEx = ctypes.windll.shell32.ShellExecuteEx ShellExecuteEx.restype = ctypes.wintypes.BOOL sei = SHELLEXECUTEINFO() sei.cbSize = ctypes.sizeof(sei) sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_INVOKEIDLIST sei.lpVerb = "properties" sei.lpFile = "C:\\Desktop\\test.txt" sei.nShow = 1 ShellExecuteEx(ctypes.byref(sei)) time.sleep(5) 

The reason for calling sleep is that the properties dialog box appears as a window in the calling process. If the Python executable immediately terminates after a call to ShellExecuteEx , then there is nothing to serve the dialog, and it is not displayed.

+5
source

All Articles