The os.getpass()
function in Python is used for security purposes. It allows user to type their password without echoing it. This means it does not display the typed characters.
It is used to secure the user’s credentials on the underlying application. In this way, the user’s security is maintained without compromising it. The getpass module has two methods:
getpass.getuser()
getpass.getpass()
1. getpass().getuser()
This method is used to get the input username as it is hidden with encryption and some secret passwords are used to keep the usernames secure.
Code
#Import getpass module
import getpass
#Print the entered username
print(getpass.getuser())
- Line#4:
getpass.getuser()
it gets the current user of the system. i.e root
2. getpass.getpass()
The getpass.getuser()
method is used to get the value of the password as a string as it is secure with encryption.
Code
# Import getpass module
import getpass
# Password variable holds the value
# of the password as string
password = getpass.getpass()
# Print the password
print("You entered",password)
- Line#5:
getpass.getpass()
get the value of the password as a string. - Line#7: It will print the input password on the console.