Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

I get 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 a file and dst is an existing directory. What I'm doing wrong?

like image 867
Jacobian Avatar asked Jun 10 '15 19:06

Jacobian


2 Answers

You're using the wrong function. You might want "copy":

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

like image 141
John Estess Avatar answered Oct 01 '22 03:10

John Estess


You have already answered yourself in the question.

dst should be the path to the copied file. So if you want to copy the file to /var/lib/my/ and your file is called f1 then dst should be /var/lib/my/f1.txt

Try to use shutil.copy as suggested here by john-estess

shutil.copy(src, dst)

or try to fix this using the following snippet

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

Assuming src is the path of the file you want to copy, such as /var/log/apache/access.log, and dst is the path to the directory, where you want to copy the file, for example, /var/lib/my then the new destination is /var/lib/my/access.log.

like image 31
boaz_shuster Avatar answered Oct 01 '22 02:10

boaz_shuster