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.
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
This works on both Windows and Linux:
On Unix or Linux:
on Windows:
and to add in your command:
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: