using shutil.copyfile I get a Python IOError: [Errno 13] Permission denied:

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.


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.


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])