pythonwindowssubprocesslnk

Open a Windows shortcut file `.lnk` with subprocess


I am fairly new to Python and have been trying to make a program that will open the "Minecraft launcher" for me.

Context

However, the new launcher .exe file is blocked as it is located in the windowsapp file which requires a lot of faffing about that I would rather avoid, so instead I was hoping to see if I could open the desktop shortcut instead to open the launcher directly?

Error

This code so far doesn't work as it gives me the error:

OSError: [WinError 193] %1 is not a valid Win32 application

Code

import time
import subprocess

subprocess.Popen('C:/Users/(my username)/Desktop/Minecraft Launcher.lnk')

I have tried subprocess.call however that doesn't seem to work either.


Solution

  • .lnk files are interpreted by the shell. So, enable the shell:

    subprocess.call("C:\\Users\\My Username\\Desktop\\Minecraft Launcher.lnk", shell=True)
    

    As a side note, the shell is one of the very few things in Windows that insists on backslashes.