how to pass scripts to python -c

Guillermo Fernandez Castellanos guillermo.fernandez at
Mon Feb 9 12:45:12 CET 2004

As I understood your question, you want to pass information to your python 
program from your command line.

python < file.txt
python file1.txt file2.txt
cat file1 file2 | python
(in this last case you write what you want and must push Crt-D to make what you 
wrote echoed to the terminal, and you can start again).

You can do this with the library fileinput.

This is a sample code that works and do what you expect from the last examples. 
It's a very simple cat program:

#### ####
import fileinput
if __name__=='__main__':
     for line in fileinput.input():
         print line,



Daniel Kramer wrote:
> what are the formatting rules to passing python commands to the python
> command line?  I've tried the following, which works:
> echo hello | python  -c "import sys; print[:4]"
> I'm actually trying to shell out of another scripting lang that's not
> very good at string parsing to have python do some work.. the only
> problem is this other lang doesn't like the ";" in my python command
> string and fails.  Is there another notation I can use on a single
> line to tell python that there is a line break?
> I tried:
> echo hello | python  -c "import sys\n print[:4]"
> but that doesn't work
> any suggestions? 
> thanks 
> daniel

More information about the Python-list mailing list