Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30443 > unrolled thread
| Started by | 叶佑群 <ye.youqun@eisoo.com> |
|---|---|
| First post | 2012-09-29 08:34 +0800 |
| Last post | 2012-09-29 08:34 +0800 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: how to run shell command like "<<EOT .... EOT" 叶佑群 <ye.youqun@eisoo.com> - 2012-09-29 08:34 +0800
| From | 叶佑群 <ye.youqun@eisoo.com> |
|---|---|
| Date | 2012-09-29 08:34 +0800 |
| Subject | Re: how to run shell command like "<<EOT .... EOT" |
| Message-ID | <mailman.1594.1348879764.27098.python-list@python.org> |
于 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)
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?
>> and pexpect code is:
>>
>> child = pexpect.spawn ("sfdisk -uM /dev/sdb<<EOT")
>> child.sendline (....)
>> child.sendline (....)
>> child.sendline (....)
>>
>> and os.popen like this:
>>
>> os.popen ("sfdisk -uM /dev/sdb<<EOT\n,1000,83\n,,83\nEOT\n")
>>
>> I tried "\r\n", and it doesn't work either.
>>
Back to top | Article view | comp.lang.python
csiph-web