0

I'm trying to run an exe through python and dump both user input and output to a log file.

Source file for exe:

//test.c
#include<stdio.h>
void main()
{
    int a=0;
    printf("Enter no:\n");
    scanf("%d",&a);
    printf("You entered %d",a);
}

after compiling the above file, I tried to run test.exe through python

#Dumpinputoutput.py
file="log.txt"
fo=open(file, "w")
subprocess.call(["test.exe"],stdout=fo,stderr=fo)
fo.close()

On executing the script,

log.txt was:

Enter no:

You entered 54

Instead of

Enter no:
54
You entered 54

I want the user's input also to be included in the log file, is it possible?
I tried subprocess.peopen also, but getting the same.

everlasto
  • 4,890
  • 4
  • 24
  • 31
  • 2
    [script](http://unixhelp.ed.ac.uk/CGI/man-cgi?script) does exactly that, but isn't available on Windows (except maybe through [cygwin](http://www.cygwin.com/)) and isn't Python. – Phil Frost Dec 31 '12 at 17:39
  • @PhilFrost: I installed cygwin, it doesn't have script command, I tried installing that command by, setup.exe -q -P script .. but still it shows command not found.. ?? – everlasto Dec 31 '12 at 19:36

1 Answers1

0

The problem is the echoed "54" you are looking for in the log file is a function of cmd.exe, not the exe you are running (see the /Q switch on cmd.exe).

What you need is a way to emulate a cmd.exe when controlling the app. Look at this answer for information about WExpect: There is WExpect for Python.

That package will let you emulate a terminal which should allow you to capture all of your interactions with the exe.

Community
  • 1
  • 1
David K. Hess
  • 16,632
  • 2
  • 49
  • 73