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?
You're using the wrong function. You might want "copy":
https://docs.python.org/2/library/shutil.html
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With