One line command line filter

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Sep 5 19:18:37 EDT 2011


Terry Reedy wrote:

> The doc says "-c <command>
> Execute the Python code in command. command can be one or more
> statements separated by newlines,"
> 
> However, I have no idea how to put newlines into a command-line string.

I imagine that it depends on the shell you are using, but bash on Linux
makes it simple: double quotes "..." are like Python's triple-quoted
strings in that they can include newlines.

[steve at sylar python]$ ls f*.py | python -c "import sys
> print sys.stdin.read()"
factorial.py
fetchqm.py
fib.py
fileutils.py
findsingle.py
fixascii.py
fix.py
frange.py
frequencies.py


Other shells may be different.


-- 
Steven




More information about the Python-list mailing list