[英]Force user input via command line in python
假設我從命令行運行腳本;
$python hello.py
此hello.py腳本將顯示“你好,歡迎光臨!” 在下一行中,它提示您輸入名稱。 例。
$python hello.py
"Hello, welcome!"
What is you name?:<here you enter the name and press enter for the program to continue>
相反,我想在調用腳本時解析我的名字。
$python hello.py <do something to parse the name>
我知道我們可以導入sys,並且可以通過args [1]來調用名稱。
我想知道是否存在一種無需修改代碼和更改我們通過命令行運行腳本的方式的方法。
您可以將答案傳遞給腳本:
echo "John Smith" | python hello.py
或使用此處字符串:
python hello.py <<<"John Smith"
如果您要提供多行輸入,那么這里文檔更容易:
python hello.py <<EOF
Answer to first question
Answer to second question
and so on
EOF
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.