How to get Desktop location? How to get Desktop location? python python

How to get Desktop location?


On Unix or Linux:

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

on Windows:

import osdesktop = 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 osdesktop = 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"))