• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get Username in Python using os module

Get Username in Python using os module

August 29, 2022 Leave a Comment

To get the current username in Python, the easiest way is with the os module getlogin() function.

import os

print(os.getlogin())

#Output:
The Programming Expert

Another way you can get the current username is from the dictionary of environment variables of the operating system.

import os

print(os.environ.get("USERNAME"))

#Output:
The Programming Expert

One other way you can get the username is with the os module path.expanduser() function.

import os

print(os.path.expanduser("~"))

#Output:
C:\Users\The Programming Expert

In Python, the os module provides us with many useful functions which allow us to get information about the operating system and environment we are working with.

One piece of information which can be valuable is the current logged in user and the username of that user.

There are a few ways you can get the current username in Python.

The easiest way to get the name of the current user in Python is with the os module getlogin() function.

Below shows a simple example of using getlogin() to get the current username in Python.

import os

print(os.getlogin())

#Output:
The Programming Expert

Another way you can get the current username is from the dictionary of environment variables of the operating system.

You can access the dictionary of environment variables with the environ dictionary.

Then, to get the username, use get() to get the username value.

Below shows you how to use the dictionary of environment variables to get the username in Python.

import os

print(os.environ.get("USERNAME"))

#Output:
The Programming Expert

One other way you can get the username is with the os module path.expanduser() function.

You can use this method if you want to get the root of the user path.

Below shows you how to use path.expanduser() to get the username in Python.

import os

print(os.path.expanduser("~"))

#Output:
C:\Users\The Programming Expert

Hopefully this article has been useful for you to get the current username in Python.

Other Articles You'll Also Like:

  • 1.  Python Get Operating System Information with os and platform Modules
  • 2.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 3.  Using Python to Find Closest Value in List
  • 4.  How to Rotate a List in Python
  • 5.  Divide Each Element in List by Scalar Value with Python
  • 6.  Get First Digit in Number Using Python
  • 7.  Python Check if Object Has Attribute
  • 8.  Get Last Day of Month Using Python
  • 9.  Union of Lists in Python
  • 10.  How to Convert pandas Column dtype from Object to Category

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy