python - Getting a hidden password input

ID : 10006

viewed : 35

Tags : pythonpasswordsuser-inputpython

Top 5 Answer for python - Getting a hidden password input

vote vote


Use getpass.getpass():

from getpass import getpass password = getpass() 

An optional prompt can be passed as parameter; the default is "Password: ".

Note that this function requires a proper terminal, so it can turn off echoing of typed characters – see “GetPassWarning: Can not control echo on the terminal” when running from IDLE for further details.

vote vote


import getpass  pswd = getpass.getpass('Password:') 

getpass works on Linux, Windows, and Mac.

vote vote


Use getpass for this purpose.

getpass.getpass - Prompt the user for a password without echoing

vote vote


This code will print an asterisk instead of every letter.

import sys import msvcrt  passwor = '' while True:     x = msvcrt.getch()     if x == '\r':         break     sys.stdout.write('*')     passwor +=x  print '\n'+passwor 
vote vote


Updating on the answer of @Ahmed ALaa

# import msvcrt import getch  def getPass():     passwor = ''     while True:         x = getch.getch()         # x = msvcrt.getch().decode("utf-8")         if x == '\r' or x == '\n':             break         print('*', end='', flush=True)         passwor +=x     return passwor  print("\nout=", getPass()) 

msvcrt us only for windows, but getch from PyPI should work for both (I only tested with linux). You can also comment/uncomment the two lines to make it work for windows.

Top 3 video Explaining python - Getting a hidden password input