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

Alan Gauld alan.gauld at yahoo.co.uk
Sat Aug 26 03:21:12 EDT 2017


> 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.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list