24

I have some python code using shutil.copyfile:

import os
import shutil

src='C:\Documents and Settings\user\Desktop\FilesPy'
des='C:\Documents and Settings\user\Desktop\\tryPy\Output'

x=os.listdir(src)
a=os.path.join(src,x[1])

shutil.copyfile(a,des)
print a

It gives me an error:

IOError: [Errno 13] Permission denied: 'C:\\Documents and Settings\\user\\Desktop\\tryPy\\Output'

Why don't I have permission to copy the file?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
DrDark
  • 445
  • 2
  • 4
  • 12
  • shutil.copyfile will be run using the permissions of the user who ran the script. Either run the python script using sudo or run it as root or use a different library that can do a copy and elevate itself to root if needed. – Eric Leschinski Sep 01 '16 at 14:58
  • You can see this solution : [https://stackoverflow.com/a/53894504/9533909](https://stackoverflow.com/a/53894504/9533909) solved my same problem – hassanzadeh.sd Feb 20 '19 at 07:06

3 Answers3

34

From the documentation of shutil.copyfile:

Copy the contents (no metadata) of the file named src to a file named dst. dst must be the complete target file name; look at shutil.copy() for a copy that accepts a target directory path. If src and dst are the same files, Error is raised. The destination location must be writable; otherwise, an IOError exception will be raised. If dst already exists, it will be replaced. Special files such as character or block devices and pipes cannot be copied with this function. src and dst are path names given as strings.

So I guess you need to either use shutil.copy or add the file name to des:

des = os.path.join(des, x[1])
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • oh thank you i feel so stupid now.. now im expecting an IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\user\\Desktop\\tryPy\\Output\\blatwo.docx' – DrDark Jun 30 '12 at 22:25
  • @DrDark I'm not sure about the reason, but that probably means it can't find the `Output` folder. Try `open('C:\\Documents and Settings\\user\\Desktop\\tryPy\\Output\\blatwo.docx', 'w')`: will it produce the same error? – Lev Levitsky Jun 30 '12 at 22:39
  • the thing is that the path you mantioned is the output folder so its a little weird that it says it can't find the file or directory. you have any idea what to do ? : O – DrDark Jun 30 '12 at 23:05
  • @DrDark check that the folder exists and its name doesn't have special characters. That's all Ican think of for now. If it doesn't help, maybe post a new question? with relevant code and error message – Lev Levitsky Jun 30 '12 at 23:32
5

I tried all the things here, but the issue with my code was regarding the permission of the destination folder. I created my own function for creating dir,

def mkdirs(newdir,mode=777):
    try:
        os.makedirs(newdir, mode)
    except OSError as err:
        return err

Instead of 777, later I used '0o777' the octal value, and later used shutil.copyfile(target_file,dest_file) and it worked !

Hope this helps someone, who is first creating the dir and then copying the file in it.

Ravi Bhanushali
  • 145
  • 2
  • 9
4

I advice you rather use shutil.copyfile rather than shutil.copy if you can.

With shutil.copyfile, you have to consider metadata such as writing permission.

PyMan
  • 132
  • 13
White
  • 111
  • 1
  • 8