35

Possible Duplicate:
Is there a portable way to get the current username in Python?

How can I get the username of the account by which the given script is being executed?

Community
  • 1
  • 1
defiant
  • 3,161
  • 11
  • 41
  • 65

1 Answers1

78
import getpass
print getpass.getuser()
Vladimir Lagunov
  • 1,895
  • 15
  • 15
  • "os.getlong()" cannot work properly if I switched user on *nix platforms, but the getpass solution works, thank you. – mxi1 Dec 10 '13 at 10:24
  • 3
    This ultimately looks at environment variables to determine the username, and hence might be handy for hacky scripts within a semi-trusted environment (e.g. your office) but should not be used for anything public or important. See the 'duplicate' question for some UNIX and Windows API ways of doing this properly. – Jonathan Hartley Aug 02 '14 at 16:49