Problem in using subprocess module and communicate()

vijay swaminathan swavijay at gmail.com
Sat May 21 11:56:50 EDT 2011


Hi  Gurus,

I'm having some problem in using the communicate() along with the
subprocess.I would like to invoke a command prompt and pass  on a .bat file
to execute. I went through the subprocess module and understood that using
communicate, we can send the send data to stdin.

According to the documentation
http://docs.python.org/library/subprocess.html#subprocess.call, it says,

if you want to send data to the process’s stdin, you need to create the
Popen object with stdin=PIPE.

so based on this, I have used the below function but it does not seem to
work. Am I missing something here?

import subprocess
import threading
import time

def runMonitor(command):
    retcode = subprocess.Popen([command, '\k', 'dir'],
                               cwd=
'C:\Python26\WorkSpace\FunctionExamples\src',
                               stdin = subprocess.PIPE,
                               creationflags=subprocess.CREATE_NEW_CONSOLE)

    retcode.wait()

    retcode.communicate('scripts_to_execute.bat')

t = threading.Thread(target = runMonitor, args = ("cmd.exe",))
t.start()

while t.is_alive():
    print 'Thread is still alive'
    time.sleep(0.5)
else:
    print 'Thread is dead'

Vijay Swaminathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110521/3406567f/attachment.html>


More information about the Python-list mailing list