Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30475

Re: how to run shell command like "<<EOT .... EOT"

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 <kushal.kumaran@gmail.com>
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> <CAH8GtdMW0Eji1LtsafUzY1Av7OASzTxHW6c8KS+3MNg4YvErww@mail.gmail.com> <50664543.1050809@eisoo.com>
From Kushal Kumaran <kushal.kumaran+python@gmail.com>
Date Sat, 29 Sep 2012 17:23:31 +0530
X-Google-Sender-Auth DuIlqGoGcYSwLnd4dGSCZ0IEu5g
Subject Re: how to run shell command like "<<EOT .... EOT"
To 叶佑群 <ye.youqun@eisoo.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1615.1348919640.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, Sep 29, 2012 at 6:18 AM, 叶佑群 <ye.youqun@eisoo.com> wrote:
> 于 2012-9-28 16:16, Kushal Kumaran 写道:
>>
>> On Fri, Sep 28, 2012 at 1:15 PM, 叶佑群<ye.youqun@eisoo.com>  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 none
>>> 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 someone
>>> can
>>> figure out the reason why?
>>>
>>>      The sample code of subprocess.Popen is:
>>>
>>>      command = ["sfdisk", "-uM",  target, "<<EOT", "\r\n",
>>>                  ",", 1000, ",", "83", "\r\n",
>>>                  ",", ",", "83", "\r\n", "EOT", "\r\n"]
>>>
>>>      pobj = subprocess.Popen (command, bufsize=1, \
>>>                          stderr=subprocess.PIPE, stdout=subprocess.PIPE)
>>>
>>>      res = pobj.stderr.readline ()
>>>      if res is not None and pobj.returncode != 0:
>>>          observer.ShowProgress (u"对设备 %s 分区失败!" % target)
>>>          return False
>>>
>> The "<<EOT" syntax (called a here-document) just provides input to the
>> command.  If you use the communicate method, you can provide input as
>> an argument:
>>
>> command = ["sfdisk", "-uM",  target ]
>> instructions = """
>> ,1000,83
>> ,,83
>> """
>> pobj = subprocess.Popen(command, stdin=subprocess.PIPE,
>> stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>> (output, errors) = 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.

<from your other email>

> If I want to read the output line by line and not put all output to memory 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.

-- 
regards,
kushal

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: how to run shell command like "<<EOT .... EOT" Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2012-09-29 17:23 +0530

csiph-web