0

I would like to open a windows application by using python subprocess, I can open the application in shell by command: start "C:\Windows\notepad.exe", however when I try to use subprocess it gives FileNotFoundError, I tired:

subprocess.run(['start', '"C:\\Windows\\notepad.exe"'])
subprocess.run(['start', "C:\\Windows\\notepad.exe"])
subprocess.run(['start', "C:\Windows\notepad.exe"])

Thanks in advance.

Xer
  • 493
  • 1
  • 6
  • 17
  • Why not just `subprocess.run(['C:\\Windows\\notepad.exe'])`? Are there special requirements to use `start`? – kotatsuyaki Oct 24 '22 at 05:10
  • The duplicate is less than stellar, this is a common FAQ but I suddenly can't find a better one for this particular problem. Anyway, as explained in a separate comment, you need `shell=True` because `start` is a feature of the legacy CMD shell on Windows. – tripleee Oct 24 '22 at 07:32

1 Answers1

0

I just check another related thread (python subprocess Popen environment PATH?).

EDIT:Subprocess with default options uses Shell=False, you need the Shell because you invoke a shell program. From docs.python.org :

On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

I tested without start and adding Shell=True and both seem to work.

1st option:

import subprocess
subprocess.run(['C:/Windows/notepad.exe'])

2nd option:

import subprocess
subprocess.run(['start', 'C:/Windows/notepad.exe'], shell=True)

Output:

enter image description here

Loxley
  • 103
  • 6
  • 1
    The solution works but the explanation is wrong. You need `shell=True` because `start` is a command of the CMD shell on Windows, and so without it, Windows can't find a command named `start`. – tripleee Oct 24 '22 at 07:13
  • @tripleee, According to your suggestion I have modified the paragraph, It was somewhat inaccurate. – Loxley Oct 24 '22 at 07:32