[Tutor] How to use subprocess module to get current logged in user?

boB Stepp robertvstepp at gmail.com
Sat Aug 26 22:29:20 EDT 2017


Thanks Alan (and Mats)!  It appears there are many ways to skin this cat!

On Sat, Aug 26, 2017 at 2:21 AM, Alan Gauld via Tutor <tutor at python.org> wrote:
>
>> My objective:  Determine who the currently logged in user is and
>> determine if that user is in my list of users that I have authorized
>> to use my programs.
>
> In addition to Steve and Mats answers:
>
> import os
> os.getlogin()   #-> 'alan'
> os.getuid() #-> 1001
>
> import pwd
> pwd.getpwuid(os.getuid()) #-> loads of user related stuff...
>
> You can also use the environment variables such as
>
> os.getenv('USER') #-> 'alan'
> os.getenv('HOME') #-> '/home/alan'
>
> the latter being a useful place to look for the
> individual config file... You can use os.environ
> if you prefer a dictionary style to a function.
>
> And of course
>
> os.cwd() #-> '/home/alan'
>
> For the current directory.


-- 
boB


More information about the Tutor mailing list