使用popen显示WindowsError:[错误2]系统找不到指定的文件

时间:2018-08-02 03:32:20

标签: python windows

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中的代码还是更改任何配置?

1 个答案:

答案 0 :(得分:0)

如果您想将带有参数作为一个字符串的整个命令传递给您,则应该使用shell=True参数:

process = Popen(cmd, stdout=PIPE, shell=True)

shlex.split(在导入shlex之后将命令行拆分为列表):

process = Popen(shlex.split(cmd), stdout=PIPE)

否则,带有参数的整个命令行将被视为一个文件名,并且系统自然无法找到它。