Python : Fun with subprocess.stdin

by Mandar Vaze on January 13, 2009
in Code, Python

As part of some automation work,  I needed to execute a program which requires each command to be explictely validated by the user by expliciltely selecting “y” or “n” , on the command line. Since I was processing a large number of files, I decided to use python script.

I used the subprocess module of python to invoke the external program. It was easy to capture the output sent on stdout. In order to send ‘y‘ on stdin for each iteration,  I tried sending ‘y‘ on stdin, but that would not work.  The script would hang. After discussing this, with more experienced python programmer, it was suggested that one possible reason why the script hangs is may be because the stdin buffer wasn’t flushed.  Both of us were not sure how to do that. Then it was discussed that when we run the external program from command line, we not only type ‘y‘ as response, but we also hit Enter there after, which results in flushing the stdin buffer. So may be that is what I ought to try.

To my surprise, it worked. So the solution was to pass ‘y/n‘ instead of single ‘y

Here is how my code looked like :

import subprocess
cmd = "...." # The command you wish to execute
proc = subprocess.Popen(cmd,
                       stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE)
print proc.communicate('Y\n')[0]

Debugging Python Scripts

by Mandar Vaze on December 24, 2008
in Code, Python, Tutorials

Found a great way to debug python scripts interactively using pdb aka Python Debugger. It is similar to gdb used on *nix.
Essentially you import pdb in the beginning of the script, and wherever you need to start debugging, add following statement :
pdb.set_trace()
Now you execute the script from command line, and execution will stop where you have added set_trace() call. You are presented with pdb prompt. There after it is similar to gdb commands.

Read more..

« Previous Page