Preloader
Python
  • Estimated reading time: 1 Minute

How to retrieve the Desktop path in Windows with Python

In order to retrieve the desktop directory with Python, you can built it based on the concatenation of the USERPROFILE environment variable of Windows with the OS module of Python. As you may know, the OS module provides a portable way of using operating system dependent functionality. Of our interest, the submodule of our interest is the os.path that allow you to manipulate paths on the system and the os.environ, known as a mapping object that returns a dictionary of the user's environmental variables.

To get started, we'll explain step by step what every piece of code means. The initial step is to obtain the current users directory, this can be obtained through the USERPROFILE environment variable of Windows, with python, use os.environ function to retrieve it:

import os

# Prints the current user's directory: C:\Users\sdkca
print(os.environ['USERPROFILE'])
# Or print(os.environ.get("USERPROFILE"))

Knowing this, you can automatically assume the Desktop directory of the user appending the Desktop keyword at the end of the path obtained from the mentioned environment variable. This can be made in a single line of code:

import os

desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')

# Prints: C:\Users\sdkca\Desktop
print("The Desktop path is: " + desktop)

Happy coding !

Share:
Carlos Delgado

Carlos Delgado

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Related articles
How to Call SAP OData Services with Python
8 Feb, 2026
  • Estimated reading time: 6 Minutes
How to Learn Web Scraping with Python? Step-by-Step Tutorial
15 May, 2024
  • Estimated reading time: 5 Minutes
Tips And Tricks To Master PDF Processing With Python
27 Feb, 2023
  • Estimated reading time: 5 Minutes
How We Can Use Object-Oriented Programming with Python?
20 Jan, 2023
  • Estimated reading time: 5 Minutes
In what areas should you use Python?
7 Jan, 2023
  • Estimated reading time: 4 Minutes
Our Sponsors

Our blog is proudly supported by industry-leading sponsors.