通过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>

我知道我们可以导入系统,并且可以通过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

相关内容

最新更新