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

前端 未结 12 933
别那么骄傲
别那么骄傲 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: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

提交回复
热议问题