How to use "while" within the command in -c option of python?

Ned Deily nad at acm.org
Fri Oct 12 22:17:08 EDT 2012


In article 
<CANOe_mi+VEW6T0ec-kcohOuxN7O2A8V6QbbZrzQrYVZi+b7otQ at mail.gmail.com>,
 Herman <sorsorday at gmail.com> wrote:
> I was just trying to do in a shell to quickly monitor a file. Imagine
> instead of printing hello, it is "os.system("cat somefile")", etc.
> Look like it works if i press an enter after the "import xxx". Thanks.

If you are using a POSIX-compatible shell, the canonical approach for 
use cases like this is to use a "here document", for example:

python - <<EOF
import os
while True:
    print('hello')
EOF

The "-" tells the Python interpreter to read from stdin.  The shell 
supplies the lines between the <<EOF and the matching EOF as stdin.

http://en.wikipedia.org/wiki/Here_document

-- 
 Ned Deily,
 nad at acm.org




More information about the Python-list mailing list