using python on the command line with 'here-documents' and pipes

Pekka Niiranen pekka.niiranen at wlanmail.com
Sun Nov 14 12:41:37 EST 2004


Hi,

This is from C:\Python23\Lib\site-packages\win32\Demos\pipes\cat.py:

'''cat.py
a version of unix cat, tweaked to show off runproc.py
'''

import sys
data = sys.stdin.read(1)
sys.stdout.write(data)
sys.stdout.flush()
while data:
     data = sys.stdin.read(1)
     sys.stdout.write(data)
     sys.stdout.flush()
# Just here to have something to read from stderr.
sys.stderr.write("Blah...")

# end of cat.py

-pekka-

calmar wrote:

> Hi all,
> 
> I would like to use python for a replacement for some binutils. I would
> like to be able to pipe things into python. Actually I would not like
> writing a 'script' to handle the input, but write the python commands
> right onto the command line.
> 
> It would be possible to use here-documents (for having the correct
> identation during e.g while loops) for writing python code on
> the fly, but then how and where to get the pipe output:
> 
> echo -e 'line1\nline2' | python <<EOF  ....
> 
> Does not really work...
> 
> Would anybody have an idea how to use python like e.g. awk etc. As a
> tool within, where I can handle the stdin data, and write the code right
> there (e.g. with here documents)?
> 
> 
> thanks a lot
> calmar
> 



More information about the Python-list mailing list