Is there a portable way to get the current user\'s username in Python (i.e., one that works under both Linux and Windows, at least). It would work like os.getuid
os.getuid
Combined pwd and getpass approach, based on other answers:
pwd
getpass
try: import pwd except ImportError: import getpass pwd = None def current_user(): if pwd: return pwd.getpwuid(os.geteuid()).pw_name else: return getpass.getuser()