My program first read something from stdin. After finished, ask the user to input something just like the codes shown in below:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
a = sys.stdin.readline()
print 'stdin is ' + a
b = raw_input("Input: ")
print 'user input: ' + b
When I run echo "111" | ./main.py
It output:
stdin is 111
Input: Traceback (most recent call last):
File "./main.py", line 9, in <module>
b = raw_input("Input: ")
EOFError: EOF when reading a line
Any idea how it could work?
Env: Python 2.7.11 (default, Dec 5 2015, 14:44:53) with OSX
Thanks!
edit: In order to eliminate the confusion, the code now change to the following:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:
import sys
a = ''
for line in sys.stdin:
a += line
print 'stdin is ' + a
b = raw_input("Input: ")
edit2:
Try (echo -e "something" && cat) | ./main.py
, but no luck
edit3: the best way I can think about is to pass a file descriptor instead of stdin:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:
import sys
a = ''
with open(sys.argv[1]) as f:
for line in f:
a += line
print 'stdin is ' + a
b = raw_input("Input: ")
Run as ./main1.py <(echo 'fasfasfasfa')
.
Please let me know if you have a better solution.