How to call a file inside a Windows shortcut file (.lnk) file in Python
Call and use WshShortcut, a WSH built-in object, from Python
Here, Notepad from the Start menu is used as an example.
win_shortcut.py
import win32com.client
notepad_path = "C:\\Users\\{USERNAME}\\AppData\\Roaming\\Microsoft\Windows\Start Menu\\Programs\\Accessories\\Notepad.lnk"
wshell = win32com.client.Dispatch("WScript.Shell") # <COMObject WScript.Shell>
shortcut = wshell.CreateShortcut(notepad_path)
print(shortcut.TargetPath) # C:\WINDOWS\system32\notepad.exe
Use a library called pywin32 Can be installed from pip
pip install pywin32
You can call a COM object like Wscript.CreateObject ("WScript.Shell ")
in VBScript with the following code
import win32com.client
wshell = win32com.client.Dispatch("WScript.Shell") #<COMObject WScript.Shell>
Generate WshShortcut object and get TargetPath property
shortcut = wshell.CreateShortcut(notepad_path)
print(shortcut.TargetPath) # C:\WINDOWS\system32\notepad.exe
Recommended Posts