[Python] Get the official file path of the shortcut file (.lnk)

How to call a file inside a Windows shortcut file (.lnk) file in Python

1. Conclusion

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

2. Call a COM object from Python

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>

3. Create a WshShortcut object to get shortcut information

Generate WshShortcut object and get TargetPath property

shortcut = wshell.CreateShortcut(notepad_path)

print(shortcut.TargetPath) # C:\WINDOWS\system32\notepad.exe

Recommended Posts

[Python] Get the official file path of the shortcut file (.lnk)
[Python] Get the character code of the file
Get the update date of the Python memo file.
Get the desktop path in Python
Get the script path in Python
Get the desktop path in Python
Get the file path using Pathlib
Pass the path of the imported python module
Check the existence of the file with python
Get the path to the systemd unit file
Check the path of the Python imported module
[Python3] Understand the basics of file operations
[Python Kivy] How to get the file path by dragging and dropping
Get the contents of git diff from python
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
[Python] Get the last updated date of the website
To get the path of the currently running python.exe
Convert the character code of the file with Python3
[Python] Get the day of the week (English & Japanese)
the zen of Python
I want to get the path of the directory where the running file is stored.
Get the size of the image file on the web (Python3, no additional library required)
Template of python script to read the contents of the file
How to get the number of digits in Python
[Python] Get the text of the law from the e-GOV Law API
Get the absolute path of the script you are running
[python] Get the list of classes defined in the module
Get the return code of the Python script from bat
Get the size (number of elements) of UnionFind in Python
[Python] Get the list of ExifTags names of Pillow library
Get the operation status of JR West with Python
Get the full path referenced by .lnk with wsl
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Towards the retirement of Python2
Summary of python file operations
Download the file in Python
About the ease of Python
Get the number of digits
[Python] Get the previous month
About the features of Python
Maya | Get the workspace path
The Power of Pandas: Python
Try to get the function list of Python> os package
Get the MIME type in Python and determine the file format
Get the number of specific elements in a python list
[Note] Import of a file in the parent directory in Python
[Python] Get the script execution directory with an absolute path
Google search for the last line of the file in Python
Get the index of each element of the confusion matrix in Python
Get the source of the page to load infinitely with python.
Python> library> os> os.walk ()> Get directory structure / Implementation to get each file path in the specified directory
Extract the xz file with python
The story of Python and the story of NaN
File / folder path manipulation in Python
Easy encryption of file contents (Python)
Get the number of views of Qiita
[Python] The stumbling block of import
Note: Get the first and last items of Python OrderedDict non-destructively