Joymaker
Joymaker

Reputation: 1420

Python subprocess.call can't open Notepad.exe?

import subprocess

subprocess.call(['C:\Windows\System32\notepad.exe'])

Leads to error:

Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver_sandbox.py", line 3, in pass File "c:\Python27\Lib\subprocess.py", line 172, in call return Popen(*popenargs, **kwargs).wait() File "c:\Python27\Lib\subprocess.py", line 408, in init errread, errwrite) File "c:\Python27\Lib\subprocess.py", line 663, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified

But I can run Notepad using that exact path from the filename bar of a folder window. What am I missing?

Upvotes: 0

Views: 1731

Answers (2)

Mitesh
Mitesh

Reputation: 1572

Here's the code that might work for you subprocess.Popen(['C:\\Windows\\System32\\notepad.exe'])

Upvotes: 0

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137448

The problem is the unescaped backlashes in your path. Python interprets '\n' as a single newline character.

Either escape the backslashes:

'C:\\Windows\\System32\\notepad.exe'

Or (preferred) use a raw string with an r prefix:

r'C:\Windows\System32\notepad.exe'

Upvotes: 3

Related Questions