Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'output': 0.04; '"""': 0.05; 'lines,': 0.05; 'emulate': 0.07; 'exit': 0.07; 'method,': 0.07; 'python': 0.09; 'argument:': 0.09; 'blocking': 0.09; 'command.': 0.09; 'exception,': 0.09; 'path.': 0.09; 'received :mail-vb0-f46.google.com': 0.09; 'res': 0.09; 'sep': 0.09; 'subject:command': 0.09; 'target,': 0.09; 'cc:addr:python-list': 0.10; 'sat,': 0.15; '"<>': 0.16; '"by': 0.16; '"use': 0.16; 'binary,': 0.16; 'code?': 0.16; 'errors)': 0.16; 'failed.': 0.16; 'fine.': 0.16; 'subject:run': 0.16; 'subprocess': 0.16; 'wrote:': 0.17; 'shell': 0.18; '>>>': 0.18; 'input': 0.18; 'memory': 0.18; 'received:209.85.212.46': 0.18; 'sender:addr:gmail.com': 0.18; 'skip:p 30': 0.20; 'variable': 0.20; 'all,': 0.21; 'pipe': 0.22; 'cc:2**0': 0.23; 'work.': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'command': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'possibility': 0.27; 'wonder': 0.27; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'buffers': 0.29; 'subject:like': 0.29; 'fri,': 0.30; 'figure': 0.30; 'located': 0.31; 'code': 0.31; 'file': 0.32; 'could': 0.32; 'print': 0.32; 'interaction': 0.33; 'traceback': 0.33; 'problem': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'false': 0.35; 'path': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'child': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'why': 0.37; 'communicate': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'skip:o 20': 0.38; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'time,': 0.62; 'is.': 0.62; 'provide': 0.62; 'mentioned': 0.63; 'behavior': 0.64; 'skip:\xe5 10': 0.65; 'subject:....': 0.65; 'due': 0.66; '8bit%:94': 0.71; 'filling': 0.75; 'process."': 0.84; 'why?': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=Z+MCEsfoJonXB+jcUUQ140zKGzG8yQ/A2oaEkha/Z8U=; b=gfbWvdKua/JRqv+y1aNw2S656FqJHaezMGLic9r5l+VWznor5jNsi8RM26x/ClNtc3 JN6BrFKOz1VNxAK1rbNBGRYkpGD9i74TG4L4yWgL3cphkUi2TrxD9VrgUwZBSxpVEY7U kEzQP+qxvKfKJfof4fUEB8i0DAmQYMdiZAwE8ByXfmOurARJbvJb8eDmy9UQ6zNam1KC aLK9kyMWKjLGn4AsuFLolw7F5PQ2D6CQGErU0Uxuuox0UthQ6P86ksIEqowNcm0yZM0Z ol2EFtPhG0QiDE4cVlCBz0rM7Ti03jyvmmn8p7TcK5HTxs538gpHT9RUcwkHiMUEhVED S/Ag== MIME-Version: 1.0 Sender: kushal.kumaran@gmail.com In-Reply-To: <50664543.1050809@eisoo.com> References: <506555B6.5040406@eisoo.com> <50664543.1050809@eisoo.com> From: Kushal Kumaran Date: Sat, 29 Sep 2012 17:23:31 +0530 X-Google-Sender-Auth: DuIlqGoGcYSwLnd4dGSCZ0IEu5g Subject: Re: how to run shell command like "< 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.15 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: 95 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348919640 news.xs4all.nl 6902 [2001:888:2000:d::a6]:38376 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30475 On Sat, Sep 29, 2012 at 6:18 AM, =E5=8F=B6=E4=BD=91=E7=BE=A4 wrote: > =E4=BA=8E 2012-9-28 16:16, Kushal Kumaran =E5=86=99=E9=81=93: >> >> On Fri, Sep 28, 2012 at 1:15 PM, =E5=8F=B6=E4=BD=91=E7=BE=A4 wrote: >> >>> Hi, all, >>> >>> I have the shell command like this: >>> >>> sfdisk -uM /dev/sdb<< EOT >>> ,1000,83 >>> ,,83 >>> EOT >>> >>> >>> I have tried subprocess.Popen, pexpect.spawn and os.popen, but non= e >>> of >>> these works, but when I type this shell command in shell, it is works >>> fine. >>> I wonder how to emulate this type of behavior in python , and if someon= e >>> can >>> figure out the reason why? >>> >>> The sample code of subprocess.Popen is: >>> >>> command =3D ["sfdisk", "-uM", target, "<>> ",", 1000, ",", "83", "\r\n", >>> ",", ",", "83", "\r\n", "EOT", "\r\n"] >>> >>> pobj =3D subprocess.Popen (command, bufsize=3D1, \ >>> stderr=3Dsubprocess.PIPE, stdout=3Dsubprocess.= PIPE) >>> >>> res =3D pobj.stderr.readline () >>> if res is not None and pobj.returncode !=3D 0: >>> observer.ShowProgress (u"=E5=AF=B9=E8=AE=BE=E5=A4=87 %s =E5=88= =86=E5=8C=BA=E5=A4=B1=E8=B4=A5=EF=BC=81" % target) >>> return False >>> >> The "<> command. If you use the communicate method, you can provide input as >> an argument: >> >> command =3D ["sfdisk", "-uM", target ] >> instructions =3D """ >> ,1000,83 >> ,,83 >> """ >> pobj =3D subprocess.Popen(command, stdin=3Dsubprocess.PIPE, >> stdout=3Dsubprocess.PIPE, stderr=3Dsubprocess.PIPE) >> (output, errors) =3D pobj.communicate(instructions) > > I tried this, but it is still not work. > What do you mean by "not work"? - If you get an exception, copy the entire traceback into an email - If you do not get an exception, print out the value of the "errors" variable to see why the command failed. You can also check pobj.returncode for the exit status of the subprocess. A possibility is that you have to replace "sfdisk" with the full path to the binary, if it cannot be located on the PATH. So you will replace it with "/usr/sbin/sfdisk", or "/sbin/sfdisk", or wherever the file actually is. > If I want to read the output line by line and not put all output to memor= y buffer in one > time, how to write the code? You can read line by line by calling pobj.stdout.readline() and pobj.stderr.readline(). You can send input to the process by calling pobj.stdin.write(). If you manage this interaction "by hand", you should not call communicate(). Also, you should be aware of the problem mentioned in the subprocess documentation: "Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process." Is there any reason why you need to read line-by-line? You could use communicate(), and then call stdout.splitlines() to get a list of lines, if that's all you need. --=20 regards, kushal