---
title: "Getting Home Path and Username in Python"
description: ", were just an one liners for me so for : os.path.expanduser(’/’) and pwd.getpwuid(os.getuid())[0] were doing the job for."
url: "https://h3manth.com/scribe/getting-home-path-and-username-python/"
canonical_url: "https://h3manth.com/scribe/getting-home-path-and-username-python/"
markdown_url: "https://h3manth.com/scribe/blog/getting-home-path-and-username-python.md"
publishDate: "2012-07-05T00:00:00.000Z"
author: "Hemanth HM"
category: "python"
tags: ["python"]
word_count: 155
reading_time_minutes: 1
x_markdown_tokens: 374
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Getting Home Path and Username in Python

> , were just an one liners for me so for : os.path.expanduser(’/’) and pwd.getpwuid(os.getuid())[0] were doing the job for.

Getting home path and username in python, were just an one liners for me so for : os.path.expanduser("~/") and pwd.getpwuid(os.getuid())[0] were doing the job for me, but when a friend was stuck with doing the same for multiple platforms he was facing few issues, so played a bit with it and came up with some silly code that worked for him, thought it was wroth sharing the code snippet with you guys, hoping it will get better!

**Getting the home path :**

import os

home = os.curdir

if 'HOME' in os.environ:
    
    
    home = os.environ['HOME']
    

elif os.name == 'posix':
    
    
    home = os.path.expanduser("~/")
    

elif os.name == 'nt':
    
    
    if 'HOMEPATH' in os.environ and 'HOMEDRIVE' in os.environ:
        home = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
    

else:
    
    
    home = os.environ['HOMEPATH']
    

**Code for getting the current user :**

import os

for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
    
    
    user = os.environ.get(name)
    if user:
        return user
    
     # If not user from os.environ.get()
     import pwd
     return pwd.getpwuid(os.getuid())[0]
