如何使用ipython调试使用stdin的脚本?

Ree*_*ece 7 python ipython

我有一个python脚本,它接受stdin的输入.我想放入IPython.embed(),像这样:

for filepath in sys.stdin:
    dir = os.path.basename(filepath)
    ...
    IPython.embed()
Run Code Online (Sandbox Code Playgroud)

然后我调用这样的脚本:

find . -type f | thescript.py
Run Code Online (Sandbox Code Playgroud)

问题是IPython使用stdin作为交互式控制台,所以它看到的第一件事就是剩下的管道数据.然后,管道关闭,终端退出.

有没有办法调试使用stdin和ipython的脚本?

tom*_*tom 5

您可以先将stdin输入读入列表,然后重置stdin:

stdin_list = list(sys.stdin)
sys.stdin = open('/dev/tty')
for filepath in stdin_list:
    dir = os.path.basename(filepath)
    ...
    IPython.embed()
Run Code Online (Sandbox Code Playgroud)