I am trying to figure out how to prompt for a password, and have the users input echoed back as asterisks (********
)
So recently, I took on the project
I think this way is very simple and effective
import sys
import msvcrt
passwor = ''
while True:
x = msvcrt.getch()
if x == '\r':
break
sys.stdout.write('*')
passwor +=x
print '\n'+passwor
You should be able to erase an asterisk by writing the characters \x08 \x08
. The \x08
will move the cursor back one position, the space will overwrite the asterisk, then the last \x08
will move the cursor back again, putting it in the correct position to write the next *
.
I don't know off the top of my head how to determine when a backspace is typed, but you can do that easily: just add something like print repr(x)
after you've called x = msvcrt.getch()
, then start your program and hit backspace.