[英]using popen shows WindowsError: [Error 2] The system cannot find the file specified
Windows 7 python 2.7当我使用popen打开一个进程时:
from ctypes import *
dldtool = cdll.LoadLibrary(r'main.dll')
cmd = "dld_tool -c {} -r programmer.bin -f {}".format(port,file)
print cmd
with LOCK:
process = Popen(cmd, stdout=PIPE)
while process.poll() is None:
out = process.stdout.readline()
if out != '':
print out
发生错误:
process = Popen(cmd, stdout=PIPE)
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
main.dll在工作目录中。 我应该更改python中的代码还是更改任何配置?
如果要以参数作为一个字符串传递整个命令,则应使用shell=True
参数:
process = Popen(cmd, stdout=PIPE, shell=True)
或shlex.split
将您的命令行拆分为一个列表(在导入shlex
):
process = Popen(shlex.split(cmd), stdout=PIPE)
否则,带有参数的整个命令行将被视为一个文件名,系统自然无法找到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.