“在Windows上使用wexpect python模块错误找不到命令...”[复制]

问题描述 投票:1回答:1

这个问题与以下内容完全相同:

所以我正在尝试与.exe文件(tcpclient.exe)进行交互,并且pexpect文档显示它是完美的,因为我需要发送多个输入并与程序交互。

但我甚至无法使用以下代码

import wexpect

child = wexpect.spawn('dir')
print child

我假设任何变量都可以print()ed,前提是函数有效。

以下是错误消息

Traceback (most recent call last):

File "test1.py", line 13, in <module>

child = wexpect.spawn('dir')

File "C:\pytest\wexpect.py", line 279, in spawn
return spawn_windows(command, args, timeout, maxread, searchwindowsize, logfile, cwd, env)

File "C:\pytest\wexpect.py", line 1653, in __init__
    self._spawn (command, args)

File "C:\pytest\wexpect.py", line 1698, in _spawn

raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)

ExceptionPexpect: The command was not found or was not executable: dir.

我正在使用windowsXP我安装了MinGW和Python2.7我通过pywin32包使用wexpect

pywin32-217.win32-py2.7.exe是安装.exe名称。

我的Python路径设置为C:\ Python27 \我尝试将其设置为C:\ Python27 \ bin,就像有人提到的那样,但之后我无法执行python。我查看了wexpect的源代码,并且有一个函数“which()”,它返回“无”到它的输入“dir”我无法修改它来做其他事情。

请告诉我我做错了什么。

提前致谢。

python windows-xp pexpect
1个回答
0
投票

'dir'是cmd.exe的内部命令,因此wexpect无法找到它。

您需要一个可执行文件的绝对路径和一个参数列表。

在这种情况下,使用cmd执行dir。

>>> import wexpect
>>> child = wexpect.spawn('C:\windows\system32\cmd.exe',['/c','dir'])
>>> print child
<wexpect.spawn_windows object at 0x024926B0> version: 2.3 ($Revision: 399 $)
command: C:\windows\system32\cmd.exe
args: ['C:\\windows\\system32\\cmd.exe', '/c', 'dir']
---snip
© www.soinside.com 2019 - 2024. All rights reserved.