46

I'm using Python on Windows and I want a part of my script to copy a file from a certain directory (I know its path) to the Desktop.

I used this:

shutil.copy(txtName, '%HOMEPATH%/desktop')

While txtName is the txt File's name (with full path).

I get the error:

IOError: [Errno 2] No such file or directory: '%HOMEPATH%/DESKTOP'

Any help?

I want the script to work on any computer.

martineau
  • 119,623
  • 25
  • 170
  • 301
Ben L
  • 743
  • 2
  • 7
  • 15
  • 2
    All answers (except GPCracker) are incorrect, because the desktop folder can be moved outside HOMEPATH. – andreymal Aug 30 '19 at 08:56

9 Answers9

61

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)
  • @BenL please vote up or accept if you found it helpful or if it solved your question. –  Dec 14 '15 at 20:36
  • both you and the one above you had great answers. wish I could accept to you both. voted up now. thank you! – Ben L Dec 14 '15 at 20:49
  • 3
    In both Windows and Linux this seems to work: `os.path.expanduser("~/Desktop")` – dashesy Apr 26 '18 at 03:36
  • 4
    There is no need for the inner `os.path.join()` call. Also, the first solution works for Windows too: there is no need for the second solution. – Eric O. Lebigot Aug 24 '18 at 08:00
  • @ChauLoi yes it is, I tested it on Mac, it gives you the parent folder of user – Idhem May 04 '20 at 13:45
  • This is no longer reliable on Windows 10. The Desktop may be located in OneDrive, either a personal account or business. – killjoy May 26 '22 at 16:41
35

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"))
dashesy
  • 2,596
  • 3
  • 45
  • 61
  • 1
    Would this work on non-English Windows machines? When I checked back around 2010 this was not the case. – Eric O. Lebigot Aug 24 '18 at 08:00
  • @EricLebigot I do not have non-English Windows machine to test, can you let us know? – dashesy Aug 24 '18 at 18:17
  • 1
    That will produce `'C:\\Users\\user/Desktop'` on Windows ... This will work on Win `desktop = os.path.expanduser("~\\Desktop")` –  Jan 03 '19 at 14:37
  • 1
    @Sabrina that is valid, in newer Widows you can use `/` and it is preferred because you won't have to escape it. You can use `os.path.normpath` if you care. – dashesy Jan 03 '19 at 18:07
  • 1
    It does work on non-English (Spanish) machine, and it actually outputs what Sabrina says. – Javier Jan 29 '19 at 12:35
  • It does not work on non-English Linux machine, because my desktop folder is actually `/home/andreymal/Рабочий стол` – andreymal Aug 30 '19 at 08:58
32

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"))
martineau
  • 119,623
  • 25
  • 170
  • 301
tpearse
  • 404
  • 4
  • 6
30

For 3.5+ you can use pathlib:

import pathlib

desktop = pathlib.Path.home() / 'Desktop'
johnson
  • 3,729
  • 3
  • 31
  • 32
13

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

Community
  • 1
  • 1
GPCracker
  • 151
  • 1
  • 4
6

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)

Salamandar
  • 589
  • 6
  • 16
1

Try this:

import os
file1 =os.environ["HOMEPATH"] + "\Desktop\myfile.txt" 
M_S_N
  • 2,764
  • 1
  • 17
  • 38
0

Just an addendum to @tpearse accepted answer:

In an embedded environment (c++ programm calling a python environment)

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

was the only one that worked. Seems like

os.path.expanduser("~/Desktop")

does not return a usable path for the embedded environment (at least not in my one; But some environmental settings in visual studio could be missing in my setup)

4lexKidd
  • 158
  • 1
  • 1
  • 9
0

Simple and Elegant Try this to access file in Desktop:

import os
import pathlib
import pandas as pd

desktop = pathlib.Path.home() / 'Desktop' / "Panda's" / 'data'
print(desktop) #check you path correct ?
data = pd.read_csv(os.path.joinn(desktop),'survey_results_public.csv')

Congrants!

Develop4Life
  • 7,581
  • 8
  • 58
  • 76
  • If you clean up your answer (remove pandas / mistyped os.path.joinn that does nothing because of misplaced bracket) you end up with a duplicate answer – johnson Jan 30 '23 at 11:51