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?

2
  • 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. Sep 1, 2016 at 14:58
  • You can see this solution : https://stackoverflow.com/a/53894504/9533909 solved my same problem Feb 20, 2019 at 7:06

3 Answers 3

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])
4
  • 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, 2012 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? Jun 30, 2012 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, 2012 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 Jun 30, 2012 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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.