[Tutor] pipes, lots of 'em

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 14 Mar 2001 06:18:21 -0800 (PST)


On Tue, 13 Mar 2001, Sean 'Shaleh' Perry wrote:

> On Tue, Mar 13, 2001 at 10:39:44PM -0800, Daniel Yoo wrote:
> > 
> > Hmmm... don't know about the pipes thing, but I did find a module that
> > will let you read tar archives:
> > 
> >     http://www.faqts.com/knowledge_base/view.phtml/aid/4395
> > 
> > Haven't played around with it yet though.
> > 
> > I'm very surprised not to easily find a full tar program for Python
> > though; perhaps someone else can verify this?
> 
> I am not surprised, there are no C libs either.  tar seems to only be spoken
> by tar.  But even if I had a tar module, I also need ar support.  So, I am
> still dealing with pipes.
> 
> ar p foo.ar bar|tar xfO - baz


Hello Sean!  I tried some limited experiments with pipes and popen, which
appear to work:

###
>>> output = os.popen('cat foo.txt.tar | tar xO').read()
>>> output
'hello world\012this is a file with multiple\012lines of text' 
###

I'm not quite sure how this will react to binary files, but at least this
works on text files.  Wait... according to the documentation on popen2:

    http://python.org/doc/current/lib/os-newstreams.html
    http://python.org/doc/current/lib/module-popen2.html

we should be able to handle the standard output from the pipe as binary
too, so we shouldn't run into many problems.  Can you show us what you've
tried so far?