Is there an easy way to show the properties dialog for a file in Windows using Python?
I'm trying to show the same window that pops up when you right click a file in Explorer and choose Properties.
To view information about a file or folder, right-click it and select Properties. You can also select the file and press Alt + Enter . The file properties window shows you information like the type of file, the size of the file, and when you last modified it.
To access the properties of a file or folder, right-click on it and select Properties. You can also Alt-click on a file or folder to access its properties.
To open a properties dialog box where you can add or edit all the properties at once and view document information, click Properties at the top of the page, and then click Advanced Properties.
The way to do this is to call the Windows ShellExecuteEx()
API passing the properties
verb. There are various high-level Python wrappers of this but I have not succeeded in getting any of them to work with the properties
verb. Instead I would use 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 I put in the call to sleep
is that the properties dialog is shown as a window in the calling process. If the Python executable terminates immediately following the call to ShellExecuteEx
, then there is nothing there to service the dialog and it does not show.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With