自动输入到Python提示中 [英] Automate Input Into Python Prompts
本文介绍了自动输入到Python提示中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个Python脚本,它将为我执行bash命令行程序.该程序要求用户输入两次,我希望我的脚本每次都自动输入"1".
I am trying to write a python script which will execute a bash command line program for me. This program asks for user input twice, and I want my script to automatically enter "1" each time.
我听说过这样的事情:
os.system("program < prepared_input")
如何编写prepare_input?谢谢.
How do I write prepared_input? Thanks.
推荐答案
创建两行文件:
1
1
然后在bash脚本中使用 read
来获取输入:
And use read
in the bash script to get the input:
演示:
$ cat abc
1
1
$ cat so.sh
#!/bin/bash
read data
echo "You entered $data"
read data
echo "Now you entered $data"
$ bash so.sh <abc
You entered 1
Now you entered 1
Python:
>>> import os
>>> os.system("bash so.sh < abc")
You entered 1
Now you entered 1
0
这篇关于自动输入到Python提示中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文