简体   繁体   English

Python subprocess.call无法打开Notepad.exe?

[英]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 追溯(最近一次通话):文件“ C:\\ Program Files(x86)\\ Wing IDE 101 5.0 \\ src \\ debug \\ tserver_sandbox.py”,第3行,传递文件“ c:\\ Python27 \\ Lib \\ subprocess.py” ”,行172,在调用返回Popen(* popenargs,** kwargs).wait()文件“ c:\\ Python27 \\ Lib \\ subprocess.py”,行408, init errread,errwrite)文件“ c:\\ Python27 \\ Lib \\ subprocess.py”,_ execute_child startupinfo中的第663行)WindowsError:[错误2]系统找不到指定的文件

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. Python将'\\n'为单个换行符。

Either escape the backslashes: 要么逃脱反斜杠:

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

Or (preferred) use a raw string with an r prefix: 或(首选)使用带有r前缀的原始字符串

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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