I\'m trying to read the target file/directory of a shortcut (.lnk
) file from Python. Is there a headache-free way to do it? The .lnk spec [PDF] is way over my h
It's easy as opening ".exe" file. Here also, we are going to use the os
module for this. You just have to create a shortcut .lnk
and store it in any folder of your choice. Then, in any Python file, first import the os
module (already installed, just import). Then, use a variable, say path
, and assign it a string value containing the location of your .lnk
file. Just create a shortcut of your desired application. At last, we will use os.startfile()
to open our shortcut.
Points to remember:
Now, you have completed the procedure. I hope it helps you. For additional assistance, I am leaving my code for this at the bottom.
import os
path = "C:\\Users\\hello\\OneDrive\\Desktop\\Shortcuts\\OneNote for Windows 10.lnk"
os.startfile(path)
In my code, I used path
as variable and I had created a shortcut for OneNote. In path, I defined the location of OneNote's shortcut. So when I use os.startfile(path)
, the os
module is going to open my shortcut file defined in variable path.