The getpass
module in Python yields a platform-independent approach to entering or adding passwords in a command-line program.
The getuser()
method is used to extract the current logged-in username or login name. In order to return, it checks system environment variables LOGNAME
, USER
, LNAME
and USERNAME
. If the value of the first one is set, it will return the login name. Otherwise, it will return None or raise an exception.
os.getuser()
It does not take any argument value.
It returns logged-in user names from environment variables LOGNAME
, USER
, LNAME
, and USERNAME
. If None
is set, it returns the login name from the password database.
In the code snippet, we are going to include the getpass
module and invoke the getuser()
method to get the current logged-in username.
# Show working of getpass.getuser() function# including getpass moduleimport getpass# invoking getuser() to extract# current parent process nameuser = getpass.getuser()# print it on consoleprint(user)