Search code examples
pythonpython-3.xpython-3.7python-os

Can Python execute the exe files by their shortcuts?


I have a Python 3 file named launch.py which tries to launch application using shortcuts. I have a folder named Apps in which I have shortcuts of almost every app I have in my PC.

Here is part of launch.py:

import os

cm = input("Type the file name : ")
print("Launching " + cm)
os.startfile("C:\Test\Apps\\" + cm.lower() + ".Ink")

Usage:

Type the file name : chrome

Unfortunately, this crashes the script. I have checked the existence of chrome.Ink in the Apps folder. Where am I going wrong? Can this be done?


Solution

  • To produce a slash (\), you have to write two, if you write one Python would consider it empty. Also, you wrote Ink instead of lnk. lnk means a link to the original file, so you need to write it correct.

    "C:\\Test\\Apps\\" + cm.lower() + ".lnk"

    Try this:

    import os
    
    cm = input("Type the file name : ")
    print(f'Launching {cm}')
    os.startfile("C:\\Test\\Apps\\" + cm.lower() + ".lnk")