我可以在python中没有echo的情况下获得控制台输入吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4616813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:32:03  来源:igfitidea点击:

Can i get console input without echo in python?

python

提问by

Can I get console input without echo in python?

我可以在python中没有echo的情况下获得控制台输入吗?

采纳答案by Josh Lee

Use getpass:

使用getpass

>>> from getpass import getpass
>>> getpass()
Password:
'secret'

回答by mguillech

Maybe the 'console'module is your only bet (it's kinda 'fork' of the curses module for Unix), however I haven't seen anything related to terminal echo disabling in its homepage, you might try to dig into it by yourself.

也许'控制台'模块是你唯一的选择(它有点像Unix的curses模块的'分叉'),但是我在它的主页上没有看到任何与终端回声禁用相关的东西,你可以尝试自己深入研究。

回答by Michael

There is also another solution (at least on unix systems, I don't know if this is working on Windows). Simply switch off the console output and use raw_input:

还有另一种解决方案(至少在 unix 系统上,我不知道这是否适用于 Windows)。只需关闭控制台输出并使用 raw_input:

os.system("stty -echo")
password = raw_input('Enter Password:')
os.system("stty echo")
print "\n"