1

So I'm trying to interact with a .exe file (tcpclient.exe) and pexpect docs show that it is perfect since I need to send multiple inputs and interact with the program.

But I cant even get the following code to work

import wexpect

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

I'm assuming any variable can be print()ed provided the function was valid.

The following is the error message

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.

I am using windowsXP I have MinGW installed and Python2.7 I am using wexpect through the pywin32 package

pywin32-217.win32-py2.7.exe that was the installation .exe name.

My Python path is set as C:\Python27\ I tried setting it to C:\Python27\bin as someone mentioned but then I couldnt execute python after that. I looked into the source code for wexpect and there is a function "which()" which is returning "None" to its input "dir" I was unable to modify it to do otherwise.

Please tell me what I did wrong.

Thanks in Advance.

Vikram Raghu
  • 53
  • 2
  • 6
  • `dir` is an internal command. Perhaps `wexpect` does not run `cmd.exe` by default. What happens if you run `'cmd /c dir'` instead? – jfs Mar 02 '15 at 18:57
  • Nope. Same result. Also I have an exe I mentioned. I tried running that originally but it didnt work. So I tried just getting the module to work first. I can see both pexpect.pyc and wxpect.pyc files in my Python27/Lib – Vikram Raghu Mar 02 '15 at 19:36
  • What happens if you run: `import subprocess; subprocess.check_call('cmd /c dir')`? – jfs Mar 02 '15 at 19:37
  • It gives the output correctly. I can see all the files and everything. – Vikram Raghu Mar 02 '15 at 19:44
  • Try to pass the full path including the file extension (`'.exe'`) to wexpect: `r'c:\Windows\...'`. You could also try winpexpect module. Though both modules are unsupported as far as I remember -- try to find a more recent fork on bitbucket or github. – jfs Mar 02 '15 at 19:53
  • That didnt work either. Thanks. I'll go searching – Vikram Raghu Mar 02 '15 at 20:52

1 Answers1

0

'dir' is an internal command of cmd.exe, and so wexpect can't find it.

You need an absolute path to an executable, and a list of arguments.

In this case, use cmd to execute 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
rickfoosusa
  • 1,061
  • 19
  • 26