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


Groups > comp.lang.python > #65229

Re: piping with subprocess

From Peter Otten <__peter__@web.de>
Subject Re: piping with subprocess
Date 2014-02-01 18:32 +0100
Organization None
References <d06a0b0e-3fb9-4e69-8104-bfe4a556fa0e@googlegroups.com> <mailman.6276.1391259231.18130.python-list@python.org> <b6be26ab-f0a6-4e35-9c44-6b03d1ea96e6@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.6286.1391275915.18130.python-list@python.org> (permalink)

Show all headers | View raw


Rick Dooling wrote:

> On Saturday, February 1, 2014 6:54:09 AM UTC-6, Peter Otten wrote:

>> Try to convert the example from the above page
>> 
>> """
>> output=`dmesg | grep hda`
>> # becomes
>> p1 = Popen(["dmesg"], stdout=PIPE)
>> p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
>> p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
>> output = p2.communicate()[0]
>> """
>> 
>> to your usecase. Namely, replace
>> 
>> ["dmesg"] --> ["textutil", "-convert", "html", infile, "-stdout"]
>> ["grep", "hda"] --> ["pandoc", "-f", "html", "-t", "marktown", "-o",
>>                      outfile]
>> 
>> Don't forget to set
>> 
>> infile = ...
>> outfile = ...
>> 
>> to filenames (with absolute paths, to avoid one source of error).
>> If that doesn't work post the code you wrote along with the error
>> messages.
> 
> p1 = subprocess.Popen(["textutil", "-convert", "html", file],
> stdout=subprocess.PIPE) 
> p2 = subprocess.check_call(["pandoc", "-f",
> "html", "-t", "markdown", "-o", markdown_file], stdin=p1.stdout,
> stdout=subprocess.PIPE)
> p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
> output = p2.communicate()[0]
> 
> Errors
> 
> Traceback (most recent call last):
>   File "/Users/me/Python/any2pandoc.py", line 70, in <module>
>     convert_word_file(file, markdown_file)
>   File "/Users/me/Python/any2pandoc.py", line 59, in convert_word_file
>     output = p2.communicate()[0]
> AttributeError: 'int' object has no attribute 'communicate'
> 
> I get a markdown_file created but it's empty.

Well, you replaced the Popen() from the example with a check_call() which 
uses a Popen instance internally, but does not expose it. 

I recommend that you stick as closely to the example as possible until you 
have a working baseline version. I'd try

textutil = subprocess.Popen(
    ["textutil", "-convert", "html", file], 
    stdout=subprocess.PIPE)
pandoc = subprocess.Popen(
    ["pandoc", "-f", "html", "-t", "markdown", "-o", markdown_file],
    stdin=textutil.stdout)

textutil.stdout.close()
pandoc.communicate()

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


Thread

piping with subprocess Rick Dooling <rpdooling@gmail.com> - 2014-02-01 04:19 -0800
  Re: piping with subprocess Peter Otten <__peter__@web.de> - 2014-02-01 13:54 +0100
    Re: piping with subprocess Rick Dooling <rpdooling@gmail.com> - 2014-02-01 05:54 -0800
      Re: piping with subprocess Rick Dooling <rpdooling@gmail.com> - 2014-02-01 06:00 -0800
        Re: piping with subprocess Rick Dooling <rpdooling@gmail.com> - 2014-02-01 07:35 -0800
          Re: piping with subprocess Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-01 15:59 +0000
      Re: piping with subprocess Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-01 14:28 +0000
      Re: piping with subprocess Peter Otten <__peter__@web.de> - 2014-02-01 18:32 +0100
  Re: piping with subprocess Daniel da Silva <var.mail.daniel@gmail.com> - 2014-02-01 07:40 -0500

csiph-web