0

I am trying to open a shortcut for Notepad called Notepad.lnk.

Because I want to learn how to open applications so I can use the pygui library to manipulate them.

Got FileNotFoundError

I got this error:

Traceback (most recent call last):
  File "C:\Users\{my_name}\PycharmProjects\texture-gen\main.py", line 185, in <module>
    subprocess.run(['start', shortcut_path], check=True)
  File "C:\Users\{my_name}\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 505, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\{my_name}\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\{my_name}\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

Code

This is my python code:

if __name__ == '__main__':
import os
import subprocess

cwd = os.getcwd()

shortcut_path = os.path.join(cwd, 'Notepad.lnk')
if not os.path.exists(shortcut_path):
    print('Error: "Notepad.lnk" shortcut not found')

subprocess.run(['start', shortcut_path], check=True)

Debugging

I have checked the name of the shortcut is Notepad.lnk and is in the current working directory.

Why is this not working?

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Welshy
  • 57
  • 8
  • What is `start` supposed to be? Is that a standard Windows program? – John Gordon Dec 27 '22 at 18:33
  • @JohnGordon [It starts another command window and runs the specified program](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/start). – Axe319 Dec 27 '22 at 18:50
  • So should I not use the starts command? @Axe319 – Welshy Dec 27 '22 at 18:56
  • 1
    Does this answer your question? [Open a shortcut file with subprocess](https://stackoverflow.com/questions/72692769/open-a-shortcut-file-with-subprocess) – Axe319 Dec 27 '22 at 19:00
  • 1
    In short, you need to pass in `shell=True` to `subprocess.run`. – Axe319 Dec 27 '22 at 19:00
  • @Axe319 thanks so much for the help would it be simple as assigning it a value and then .kill()? – Welshy Dec 27 '22 at 22:44
  • It should be as simple as changing `subprocess.run(['start', shortcut_path], check=True)` to `subprocess.run(['start', shortcut_path], check=True, shell=True)`. – Axe319 Dec 28 '22 at 15:54

0 Answers0