How do I convert a password into asterisks while it is being entered?
If you want a solution that works on Windows/macOS/Linux and on Python 2 & 3, you can install the pwinput
module (formerly called stdiomask):
pip install pwinput
Unlike getpass.getpass()
(which is in the Python Standard Library), the pwinput
module can display *** mask characters as you type. It is also cross-platform, while getpass
is Linux and macOS only.
Example usage:
>>> pwinput.pwinput()
Password: *********
'swordfish'
>>> pwinput.pwinput(mask='X') # Change the mask character.
Password: XXXXXXXXX
'swordfish'
>>> pwinput.pwinput(prompt='PW: ', mask='*') # Change the prompt.
PW: *********
'swordfish'
>>> pwinput.pwinput(mask='') # Don't display anything.
Password:
'swordfish'
Unfortunately this module, like Python's built-in getpass
module, doesn't work in IDLE or Jupyter Notebook.
More details at https://pypi.org/project/pwinput/
How to mask user password input and save it as output to replace default password in another file
Leaving other problems of your approach aside, I'll focus on your "Unwanted result: Masked input password didnt replace default password."
You have a global
list1
, and you define alist1
in thegetPASS()
function as well, which you want to use after returning from the function. This doesn't work as is, since thelist1
in the function is local to it and distinct from the globallist1
. I recommend to return a proper value from the function, e. g.:def getPASS():
print("Input password")
list1 = []
while True:
…
return "".join(list1)
…
psw = getPASS()The logic of password input and verification got mixed up here:
psw = "".join(list1)
print(psw)
invalid = ' ,:;/?"\}]{[-=+!@#$%^&*()|'
for x in psw:
if x in invalid:
print("Character %r is not allowed in password" % x)
getPASS()
else:
pass
getPASS()You're trying to assign and verify the password before you even call
getPASS()
to input it, thereforepsw
is empty. Rearrange that, e. g. (assuming above 1. change togetPASS()
):while True:
psw = getPASS()
print(psw) # only for debugging
invalid = ' ,:;/?"\}]{[-=+!@#$%^&*()|'
for x in psw:
if x in invalid:
print("Character %r is not allowed in password" % x)
break # from for x in psw
else:
break # from while True
Masking keyboard input with a character - Python version 3.0
I think I have just what you need.
Password is set to variable name 'psw'.
This only seems to work in cmd though.
import msvcrt
import time
import sys
import os
def getPASS():
print("Input password")
list1 = []
while True:
char = msvcrt.getch()
char =str(char)
char = char[2:-1]
if char == "\\n'" or char == "\\r":
break
elif char == "\\x08":
del list1[-1]
os.system("cls")
print("Input password:")
sys.stdout.write("*" * len(list1))
sys.stdout.flush()
continue
else:
list1.append(char)
sys.stdout.write("*")
sys.stdout.flush()
print("\n")
psw = "".join(list1)
print(psw)
invalid = ' ,:;/?"\}]{[-=+!@#$%^&*()|'
for x in psw:
if x in invalid:
print("Character %r is not allowed in password" % x)
getPASS()
else:
pass
getPASS()
Please give any improvements, Dan
Related Topics
How to Convert Datetime by Removing Nanoseconds
Django: Check Whether an Object Already Exists Before Adding
How to Get All Users in a Telegram Channel Using Telethon
Passing a List of Values from Python to the in Clause of an SQL Query
Using SQL Server Stored Procedures from Python (Pyodbc)
I Want to Reshape 2D Array into 3D Array
How to Import a File in Python With Spaces in the Name
How to Select All Elements Greater Than a Given Values in a Dataframe
Delete Rows Containing Numeric Values in Strings from Pandas Dataframe
Print All Number Divisible by 7 and Contain 7 from 0 to 100
How to Convert Data from Txt Files to Excel Files Using Python
Reading a CSV File into Pandas Dataframe With Quotation in Some Entries
Easiest Way to Convert Two Columns to Python Dictionary
How to Select the Last Column of Dataframe
How to Send Email to Multiple Recipients Using Python Smtplib
How to Split But Ignore Separators in Quoted Strings, in Python