Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i check which user is currently logged in on the computer with python?

So i wonder if there is a way to find out which user is currently logged in on the computer, and store it in a variable so i get print it out or do something else with it later. The reason why i need this is because when i enter a path including the user, it's gonna be diffrent depending on which user is logged in. I tried to look for this on the internet but i could't find what I was looking for.

OS: Windows 8.1 Version: Python 3.4 Let me know if there is anything i need to clearify.

OT: I'm sorry for any potential mistakes i've done, i'm new to this site.

Example:

checkuser = #The user currently logged in

print("The user currently logged in is: " + checkuser)

So for example if ExampleUser would be logged in it would print:

"The user currently logged in is: ExampleUser"

like image 293
Lojas Avatar asked Oct 16 '25 22:10

Lojas


1 Answers

Check this site

the answer with your snippet would be:

import getpass
checkuser = getpass.getuser()

print("The user currently logged in is: " + checkuser)

Also if you need the name just because of the home directory path you can use os.environ['HOMEPATH'] (on Windows) to get the path directly.

like image 117
kyjanond Avatar answered Oct 19 '25 12:10

kyjanond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!