Вопрос

Given the following python script....

$ cat readStdin.py 
#!/usr/bin/python

import sys

var = "".join(sys.stdin.readlines()).rstrip()
print var

... I get the follwing output:

$ echo hello  | python -i readStdin.py 
hello
>>> 
$

... in other words it does not hang in the python console, but goes back to bash. Does anyone out there know how to make it stay in the python console???

Это было полезно?

Решение

Consider this -

$ echo print  4*2 | python -i
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
Type "help", "copyright", "credits" or "license" for more information.
>>> 8
>>> 
$

Echo produces print 4*2. Python even in interactive mode, considers this as input to be interpreted. Hence we see the 8 there. After this, the interpreter encounters an EOF, so it quits. Consider what you press to quit the interpreter - Ctrl+d or ^D. This is just another way to produce EOF on *nix.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top