I'm looking for a cross platform solution for getting current login/username in Python.
I was surprised that os.getlogin()
is only supported under Unix and even there is not necessarly returning what you would expect.
I'm looking for a cross platform solution for getting current login/username in Python.
I was surprised that os.getlogin()
is only supported under Unix and even there is not necessarly returning what you would expect.
Here is what I use:
import os
username = getattr(os, "getlogin", None)
if not username:
for var in ['USER', 'USERNAME','LOGNAME']:
if var in os.environ:
username = os.environ[var]
print("username: %s" % (username))