how to pass scripts to python -c

Guillermo Fernandez Castellanos guillermo.fernandez at epfl.ch
Mon Feb 9 06:51:39 EST 2004


Oh! I found another version:

#### pycat.py ####
#!/usr/bin/env python
import sys
if len(sys.argv)==1:
     while(1):
         readed=sys.stdin.read(1)
         sys.stdout.write(readed)
else:
     for files in sys.argv[1:]:
         filed=file(files,'r')
         readed=filed.read()
         sys.stdout.write(readed)


Don't ask me why you must put read(1) and not simply read(), it works, and I'm 
happy :-)
But if someone could explain me why it works, it would be cool.

Regards,

Guille

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 sys.stdin.read()[: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 sys.stdin.read()[:4]"
> 
> but that doesn't work
>  
> any suggestions? 
> 
> thanks 
> 
> daniel



More information about the Python-list mailing list