背景:
我想模拟一下
python xxx.py < file.txt
在代码中
xxx.py
将期待
user input
这里
stdin
重定向到
file.txt
.
所以我有以下代码来模拟重定向:
Ty.Py:
import sys
f = open('file.txt', 'w')
f.write('"Tom"')
f.write('\n')
f.write('"Jerry"')
f.write('\n')
f.close()
sys.stdin = open('file.txt')
for i in range(2):
val = input()
print('Say hello to: ' + val)
sys.stdin = sys.__stdin__ # restore stdin
上面的命令在windows cmd中非常有效,如下所示:
C:\work>python try.py
Say hello to: Tom
Say hello to: Jerry
问题:
然后,我把它粘贴到
jupyter notebook
,然后单击“运行”。它仍然给我一个
input text box
让我输入
input
.
为什么
standard input redirect
我把它放进去的时候没有效果
Jupyter笔记本
?
Jupyter notebook
做些特别的事情来运行我们的代码?以后运行代码时需要注意的事项
Jupyter
?请建议。
注:
为了清楚起见,我将图片粘贴在这里,代码与上面完全相同
try.py
.