Reading the target of a .lnk file in Python?
Create a shortcut using Python (via WSH)
import sys
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut("t:\\test.lnk")
shortcut.Targetpath = "t:\\ftemp"
shortcut.save()
Read the Target of a Shortcut using Python (via WSH)
import sys
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut("t:\\test.lnk")
print(shortcut.Targetpath)
How do I get the target of a web shortcut lnk file?
You need to create the web shortcut in a slightly different way. According to this TechNet forum posting, the filename must have an extension of .url
rather than .lnk
, and you must explicitly call the .Save()
method of the object.
General approach to reading lnk files
There is not an official document from Microsoft describing lnk file format but there are some documents which have description of the format. Here is one of them: Shortcut File Format (.lnk)
As for the API you can use IShellLink Interface
Can python detect whether a shortcut file (.lnk) no longer works?
You could do this by attempting to check if the file path has an existing file. There is a solution here on SO that contains many ways to do this:
How do I check whether a file exists without exceptions?
If you check if the file exists at the path specified by the shortcut you can then detect whether the shortcut itself would work and do your own procedures for it.
Here is a quote from the link above that you could use to do this:
"If you're not planning to open the file immediately, you can use os.path.isfile
Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
import os.path
os.path.isfile(fname)
if you need to be sure it's a file."
If for some reason the file exists but your shortcut doesnt work, this wouldn't work. You could then use a try:
to open the file and except:
all the possible errors that may arise. Sometimes a shortcut won't work because of insufficient permissions or because the target
is no longer valid.
If the file exists but it won't open via shortcut then it's a problem with the shortcut file itself, the target file, an operating system bug/glitch, or insufficient permissions.
Related Topics
How to Efficiently Handle European Decimal Separators Using the Pandas Read_CSV Function
How to Install Python Packages in Google's Colab
How to Read One Single Line of CSV Data in Python
How to Use MySQLdb with Python and Django in Osx 10.6
Flask-Sqlalchemy Update a Row's Information
Fitting a Histogram with Python
How to Understand the Output of Dis.Dis
Mixing Cdef and Regular Python Attributes in Cdef Class
Overriding Properties in Python
CSV New-Line Character Seen in Unquoted Field Error
Better Way to Shuffle Two Numpy Arrays in Unison
Pandas: Valueerror: Cannot Convert Float Nan to Integer
Installing Numpy and Scipy on 64-Bit Windows (With Pip)
Thread Local Storage in Python
How to Find Out My Pythonpath Using Python
How to Get a List of All Indices of Repeated Elements in a Numpy Array
In Tensorflow, Differencebetween Session.Run() and Tensor.Eval()