Copying a file to an existing directory results in an IOError [Error 21] - This is a directory

I am getting this error:

IOError [Error 21] is a directory

      

when i try to copy a file to an existing directory. I do it like this:

shutil.copyfile(src, dst)

      

where src

is the file and dst

is the existing directory. What am I doing wrong?

+3


source to share


2 answers


You have already answered the question in the question.

dst

must be the path to the copied file. So if you want to copy a file to /var/lib/my/

, and your file is named f1

then there dst

should be/var/lib/my/f1.txt

Try to use shutil.copy

as proposed here , john-estess

shutil.copy(src, dst)

      



or try to fix it using the following snippet

shutil.copyfile(src, '%s/%s' % (dst, src.split('/')[-1]))

      

Assuming src is the path to the file you want to copy, for example /var/log/apache/access.log

and dst

is the path to the directory where you want to copy the file to, for example /var/lib/my

, and then the new destination /var/lib/my/access.log

.

+1


source


You are using the wrong function. You might want to "copy":



https://docs.python.org/2/library/shutil.html

+7


source







All Articles