Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Desktop location?

People also ask

How do I show the Desktop in File Explorer?

Open Windows Explorer. Click on Desktop in the left sidebar. Click on "Desktop" in the address bar.

Where is the Desktop folder in C drive?

The user desktops are located at C:/Users/<insert user name here>/desktop. Then the public one is at C:/Users/Public/Desktop. In Windows XP the location is C:/Documents and Settings/<insert user name here>/Desktop.

How do I get the Desktop location in Python?

You can use os. environ["HOMEPATH"] to get the path.

How do I change my Desktop location in Windows 11?

Change default location for downloads: Open the Download folder's Properties > Move. Pick a new location. You can choose any location you like, including the desktop. When you click Apply, it may ask you to move your existing files.


On Unix or Linux:

import os
desktop = os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop') 

on Windows:

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

and to add in your command:

shutil.copy(txtName, desktop)

You can use os.environ["HOMEPATH"] to get the path. Right now it's literally trying to find %HOMEPATH%/Desktop without substituting the actual path.

Maybe something like:

shutil.copy(txtName, os.path.join(os.environ["HOMEPATH"], "Desktop"))

This works on both Windows and Linux:

import os
desktop = os.path.expanduser("~/Desktop")

# the above is valid on Windows (after 7) but if you want it in os normalized form:
desktop = os.path.normpath(os.path.expanduser("~/Desktop"))

For 3.5+ you can use pathlib:

import pathlib

desktop = pathlib.Path.home() / 'Desktop'

I can not comment yet, but solutions based on joining location to a user path with 'Desktop' have limited appliance because Desktop could and often is being remapped to a non-system drive. To get real location a windows registry should be used... or special functions via ctypes like https://stackoverflow.com/a/626927/7273599


All those answers are intrinsecally wrong : they only work for english sessions.

You should check the XDG directories instead of supposing it's always 'Desktop'.

Here's the correct answer: How to get users desktop path in python independent of language install (linux)


Try this:

import os
file1 =os.environ["HOMEPATH"] + "\Desktop\myfile.txt"