简体   繁体   中英

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?

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'

这是可能适合您subprocess.Popen(['C:\\\\Windows\\\\System32\\\\notepad.exe'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM