Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:module': 0.04; 'subject:using': 0.04; 'typing': 0.05; 'paths': 0.07; "shouldn't": 0.07; '21,': 0.09; 'gurus,': 0.09; 'newline': 0.09; 'retcode': 0.09; 'says,': 0.09; 'subject:()': 0.09; 'subject:Problem': 0.09; 'subprocess': 0.09; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'proc': 0.16; 'received:209.85.213.174': 0.16; 'received:mail- yx0-f174.google.com': 0.16; 'stdin': 0.16; 'stdin,': 0.16; 'subject:subprocess': 0.16; 'terminates': 0.16; 'threading': 0.16; 'input': 0.17; 'cc:addr:python-list': 0.17; 'call.': 0.19; 'shell': 0.19; 'cheers,': 0.19; 'command': 0.19; 'header:In-Reply- To:1': 0.21; 'variable': 0.21; 'seems': 0.21; 'cc:2**0': 0.22; 'code.': 0.22; 'cc:no real name:2**0': 0.23; 'here?': 0.23; 'integer': 0.23; "skip:' 40": 0.23; 'runs': 0.23; '(or': 0.24; 'function': 0.25; 'object': 0.26; "i'm": 0.27; 'work.': 0.28; 'message-id:@mail.gmail.com': 0.28; 'problem': 0.28; 'sat,': 0.29; 'import': 0.29; 'exit': 0.29; 'cc:addr:python.org': 0.30; 'module': 0.30; 'putting': 0.30; 'blocks': 0.30; 'invoke': 0.30; 'terminate': 0.30; '\xc2\xa0\xc2\xa0\xc2\xa0': 0.30; 'url:library': 0.31; 'calling': 0.31; 'seem': 0.32; "can't": 0.32; 'does': 0.33; 'rather': 0.34; 'chris': 0.34; 'file': 0.34; 'here,': 0.35; 'using': 0.35; 'test': 0.35; 'skip:. 10': 0.36; 'several': 0.36; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'received:209.85.213': 0.37; 'url:docs': 0.37; 'communicate': 0.37; 'think': 0.38; 'url:python': 0.38; 'url:org': 0.38; 'run': 0.38; 'but': 0.38; 'data': 0.38; 'holding': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'skip:s 20': 0.39; 'should': 0.39; 'received:209': 0.39; 'version:': 0.39; 'windows.': 0.39; 'missing': 0.40; 'really': 0.40; 'your': 0.60; 'below': 0.61; 'here:': 0.62; 'according': 0.63; '8bit%:100': 0.68; 'dead': 0.80; 'forever': 0.84; 'no?': 0.84; 'sender:addr:chris': 0.84; 'together,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=iAg3MkO3CGCd4jzbFJP1Nrgy4waQpWOfV+oRyS7yYio=; b=A0ErT4Uvi3iM7yesN4jWYlIOp91iGgZKKp/jKMeJyjxbDPQrnxB8QIHDUYBnh3aF4X jTnep5NkoghkZ7dphbwVE7WxRqMz2Gvinq58cXsq4kzJMvyVc20fCwe4vQmAWNkRF490 LlnFc/oGXtA3YJTGeEZZJeiVJUW6rZkBHEKGI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=QXc350R7Xq3snOELVVgFNAZjm3jITKSrxYIyzDv06WwK7tsQH2Dc9CYJ13o/cqqyGg aS1J+TtA3SNMgBJYPPQjvJpYY16YY72NzVc74YOxwqnZ073gRNc2G2LrHHvDxpcLcShc cv1kqHgWt/UI7ovJdwcpbaH/YHUU4kQDMSkbw= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Sat, 21 May 2011 12:38:54 -0700 X-Google-Sender-Auth: ht3x1tQ1uLbVvalXSfBR_Cmj-0M Subject: Re: Problem in using subprocess module and communicate() From: Chris Rebert To: vijay swaminathan Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 81 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306006743 news.xs4all.nl 49176 [::ffff:82.94.164.166]:61000 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5934 On Sat, May 21, 2011 at 8:56 AM, vijay swaminathan wro= te: > Hi=C2=A0 Gurus, > > I'm having some problem in using the communicate() along with the > subprocess.I would like to invoke a command prompt and pass=C2=A0 on a .b= at file > to execute. I went through the subprocess module and understood that usin= g > 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=E2=80=99s stdin, you need to crea= te the > Popen object with stdin=3DPIPE. > > 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): > =C2=A0=C2=A0=C2=A0 retcode =3D subprocess.Popen([command, '\k', 'dir'], Shouldn't that be /k, not \k? > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cwd=3D > 'C:\Python26\WorkSpace\FunctionExamples\src', Sidenote: You really should use forward-slashes in paths like this (or double-up the backslashes). > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 stdin =3D subprocess.PIPE, > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 creationflags=3Dsubprocess.CREATE_NEW_CON= SOLE) > > =C2=A0=C2=A0=C2=A0 retcode.wait() > > =C2=A0=C2=A0=C2=A0 retcode.communicate('scripts_to_execute.bat') Several issues here: - .wait() waits for the subprocess to terminate; there's reason for the shell to terminate here, so your program will hang forever on the .wait() call. - It makes no sense to call .communicate() after .wait(), since the subprocess will have already terminated; you can't communicate with a dead process. - .communicate() blocks until the subprocess terminates anyway, so calling .wait() is pointless. - I would think a newline is needed in you input to the shell. When one runs a command in the shell manually, one presses Enter after typing it, no? - `retcode` seems a rather poor variable name, since it's holding a Popen object rather than an integer exit code. Putting this all together, here's an improved version: def runMonitor(command): proc =3D subprocess.Popen([command, '/k', 'dir'], cwd=3D'C:/Python26/WorkSpace/FunctionExamples/src', stdin=3Dsubprocess.PIPE, creationflags=3Dsubprocess.CREATE_NEW_CONSOLE) input_data =3D 'scripts_to_execute.bat\n' + 'exit\n' stdout_data, stderr_data =3D proc.communicate(input_data) I can't test it since I don't run Windows. Cheers, Chris -- http://rebertia.com