pythoncopydesktopfilepath

How to get Desktop location?


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.


Solution

  • 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"))