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

前端 未结 12 793
别那么骄傲
别那么骄傲 2020-11-22 03:11

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

相关标签:
12条回答
  • 2020-11-22 03:51

    For UNIX, at least, this works...

    import commands
    username = commands.getoutput("echo $(whoami)")
    print username
    

    edit: I just looked it up and this works on Windows and UNIX:

    import commands
    username = commands.getoutput("whoami")
    

    On UNIX it returns your username, but on Windows, it returns your user's group, slash, your username.

    --

    I.E.

    UNIX returns: "username"

    Windows returns: "domain/username"

    --

    It's interesting, but probably not ideal unless you are doing something in the terminal anyway... in which case you would probably be using os.system to begin with. For example, a while ago I needed to add my user to a group, so I did (this is in Linux, mind you)

    import os
    os.system("sudo usermod -aG \"group_name\" $(whoami)")
    print "You have been added to \"group_name\"! Please log out for this to take effect"
    

    I feel like that is easier to read and you don't have to import pwd or getpass.

    I also feel like having "domain/user" could be helpful in certain applications in Windows.

    0 讨论(0)
  • 2020-11-22 03:53

    These might work. I don't know how they behave when running as a service. They aren't portable, but that's what os.name and ifstatements are for.

    win32api.GetUserName()
    
    win32api.GetUserNameEx(...) 
    

    See: http://timgolden.me.uk/python/win32_how_do_i/get-the-owner-of-a-file.html

    0 讨论(0)
  • 2020-11-22 03:56

    Look at getpass module

    import getpass
    getpass.getuser()
    'kostya'
    

    Availability: Unix, Windows


    p.s. Per comment below "this function looks at the values of various environment variables to determine the user name. Therefore, this function should not be relied on for access control purposes (or possibly any other purpose, since it allows any user to impersonate any other)."

    0 讨论(0)
  • 2020-11-22 03:56

    You best bet would be to combine os.getuid() with pwd.getpwuid():

    import os
    import pwd
    
    def get_username():
        return pwd.getpwuid( os.getuid() )[ 0 ]
    

    Refer to the pwd docs for more details:

    http://docs.python.org/library/pwd.html

    0 讨论(0)
  • 2020-11-22 03:58

    Combined pwd and getpass approach, based on other answers:

    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()
    
    0 讨论(0)
  • 2020-11-22 03:58

    You can get the current username on Windows by going through the Windows API, although it's a bit cumbersome to invoke via the ctypes FFI (GetCurrentProcess → OpenProcessToken → GetTokenInformation → LookupAccountSid).

    I wrote a small module that can do this straight from Python, getuser.py. Usage:

    import getuser
    print(getuser.lookup_username())
    

    It works on both Windows and *nix (the latter uses the pwd module as described in the other answers).

    0 讨论(0)
提交回复
热议问题