Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'messages.': 0.05; 'output': 0.05; '"""': 0.07; 'bash': 0.09; 'filenames': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'variables.': 0.09; 'python': 0.11; 'wrote': 0.14; 'language.': 0.14; 'error).': 0.16; 'grep': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'spacing': 0.16; 'prevent': 0.16; 'language': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'file,': 0.19; 'thanks.': 0.20; '>>>': 0.22; 'example': 0.22; 'shell': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'replace': 0.24; 'url:moin': 0.24; "i've": 0.25; 'source': 0.25; 'script': 0.25; 'post': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; "doesn't": 0.30; 'absolute': 0.30; 'especially': 0.30; 'along': 0.30; "i'm": 0.30; 'code': 0.31; 'url:wiki': 0.31; '(on': 0.31; 'pipe': 0.31; 'file': 0.32; 'url:python': 0.33; 'becomes': 0.33; 'could': 0.34; 'subject:with': 0.35; 'skip:s 30': 0.35; 'convert': 0.35; 'but': 0.35; 'url:org': 0.36; 'half': 0.37; 'level': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'how': 0.40; 'read': 0.60; 'above,': 0.60; 'save': 0.62; 'show': 0.63; 'our': 0.64; 'receive': 0.70; 'day': 0.76; '"best': 0.84; 'attempt,': 0.84; 'fail.': 0.84; 'otten': 0.84; 'piping': 0.84; 'rick': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: piping with subprocess Date: Sat, 01 Feb 2014 14:28:41 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-92-24-219-97.ppp.as43234.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: 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: 114 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391264938 news.xs4all.nl 2950 [2001:888:2000:d::a6]:49523 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65217 On 01/02/2014 13:54, Rick Dooling wrote: > On Saturday, February 1, 2014 6:54:09 AM UTC-6, Peter Otten wrote: >> Rick Dooling wrote: >> >> >> >>> I spent half a day trying to convert this bash script (on Mac) >> >>> >> >>> textutil -convert html $1 -stdout | pandoc -f html -t markdown -o $2 >> >>> >> >>> into Python using subprocess pipes. >> >>> >> >>> It works if I save the above into a shell script called convert.sh and >> >>> then do >> >>> >> >>> subprocess.check_call(["convert.sh", file, markdown_file]) >> >>> >> >>> where file and markdown_file are variables. >> >>> >> >>> But otherwise my piping attempts fail. >> >> >> >> It is always a good idea to post your "best effort" failed attempt, if only >> >> to give us an idea of your level of expertise. >> >> >> >>> Could someone show me how to pipe in subprocess. Yes, I've read the doc, >> >>> especially >> >>> >> >>> http://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline >> >>> >> >>> But I'm a feeble hobbyist, not a computer scientist. >> >> >> >> 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. Would you please read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing the double line spacing above, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence