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

Roy Smith roy at panix.com
Sun Nov 14 13:08:23 EST 2004


calmar <calmar at calmar.ws> wrote:

> I would like to be able to pipe things into python.

Python is perfectly happy reading stdin and writing to stdout, so it's 
also perfectly happy acting as a component in a pipe.

> Actually I would not like
> writing a 'script' to handle the input, but write the python commands
> right onto the command line.

You can do this with the -c command line option:   

$ python -c 'print "hello world"'
hello world
 
> 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:

I think you're confusing two different things: where python gets the 
code it's going to execute, and where it gets the input it's going to 
feed to that executing code.  You can use here-is documents for the 
input, and -c for the code, and end up with something like this:

$ python -c 'import sys
> for line in sys.stdin:
>     print line[:10]
> 
> ' << EOF
> line one of many
> line two of many
> line three of many
> EOF
line one o
line two o
line three

Whether or not the single-quote mechanism reads past newlines and 
properly preserves indentation is more a function of what shell you're 
using than the python interpreter itself (the above was done with bash).

> 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)?

Well, the above will work, but I don't think it's a particularly good 
idea.  I think you would do better putting the text of your python 
script in a .py file, make the first line #!/usr/bin/python, and make it 
executable.  Then you've got a stand-alone program that you can edit and 
maintain apart from the shell script that surrounds it.  In the long 
run, this is likely to be simpler to test and to maintain.

Of course, if you want to be a bit perverse, you can always do:

$ python /dev/stdin << EOF
> print "hello, bizarre world"
> EOF
hello, bizarre world

but I think that's more of a curiosity than a useful tool.  Along the 
same lines, I once knew somebody who linked /dev/tty.c to /dev/tty.  He 
could then do "cc /dev/tty.c" and type in c code directly to the 
compiler".  He was a sick puppy.



More information about the Python-list mailing list