问题
I've got a python script that takes input on stdin. I'd like to drop into IPython.embed(), like this:
for filepath in sys.stdin:
dir = os.path.basename(filepath)
...
IPython.embed()
I then invoke the script like this:
find . -type f | thescript.py
The problem is that IPython uses stdin for the interactive console, so the first thing it sees are the remaining pipe data. Then, the pipe closes and the the terminal exits.
Is there a way to debug a script that uses stdin with ipython?
回答1:
You could read your stdin input into a list first, then reset stdin:
stdin_list = list(sys.stdin)
sys.stdin = open('/dev/tty')
for filepath in stdin_list:
dir = os.path.basename(filepath)
...
IPython.embed()
来源:https://stackoverflow.com/questions/13979422/how-do-i-debug-a-script-that-uses-stdin-with-ipython