Python interative mode not working when reading from std to

Given the following python script ....

$ cat readStdin.py #!/usr/bin/python import sys var = "".join(sys.stdin.readlines()).rstrip() print var 

... I get the following output:

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

... in other words, it does not hang on the python console, but returns to bash. Does anyone know how to make it stay in python console ???

+4
source share
1 answer

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 interactively sees this as input that needs to be interpreted. Therefore, we see there 8. After that, the interpreter encounters the EOF , so it exits. Consider what you press to exit the interpreter - Ctrl+d or ^D This is another way to create EOF on * nix.

+3
source

Source: https://habr.com/ru/post/1438686/


All Articles