Reading the target of a .lnk file in Python?

前端 未结 7 2086
悲&欢浪女
悲&欢浪女 2020-11-28 04:12

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

7条回答
  •  粉色の甜心
    2020-11-28 04:56

    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:

    1. The location should be within double inverted commas.
    2. Most important, open Properties. Then, under that, open "Details". There, you can get the exact name of your shortcut. Please write that name with ".lnk" at last.

    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.

提交回复
热议问题