portability - Is there a portable way to get the current username in Python? -
is there portable way current user's username in python (i.e., 1 works under both linux , windows, @ least). work os.getuid
:
>>> os.getuid() 42 >>> os.getusername() 'slartibartfast'
i googled around , surprised not find definitive answer (although perhaps googling poorly). pwd module provides relatively easy way achieve under, say, linux, not present on windows. of search results suggested getting username under windows can complicated in circumstances (e.g., running windows service), although haven't verified that.
look @ getpass module
import getpass getpass.getuser() 'kostya'
availability: unix, windows
p.s. per comment below "this function looks @ values of various environment variables determine user name. therefore, function should not relied on access control purposes (or possibly other purpose, since allows user impersonate other)."
Comments
Post a Comment