4

I am trying to copy multiple files from one directory, into another directory.

src_files = os.listdir("srcdir")
print(src_files)
for file_name in src_files:
    full_file_name = os.path.join("srcdir", file_name)
    if (os.path.isfile(full_file_name)):
        shutil.copy(full_file_name, "destdir/")

However I am getting the following error:

Traceback (most recent call last):
  File "script.py", line 539, in <module>
    buildGUI()
  File "script.py", line 385, in buildGUI
    shutil.copy(full_file_name, "destdir/")
  File "/home/opt/lib/python3.4/shutil.py", line 228, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/home/opt/lib/python3.4/shutil.py", line 108, in copyfile
    with open(dst, 'wb') as fdst:
IsADirectoryError: [Errno 21] Is a directory: '/home/destdir/'

I thought shutil.copy supported a target directory? Thats what the documentation says.

EDIT: Based on comment, think I should add that this is Python3.4 on a 64bit Debian Machine.

EDIT2: As a workaround I'll just use shutil.copytree("sourcedir", "destdir/") which seems to work. I'll leave this question open, and file a bug report or something unless an answer is posted.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
Sam Coulter
  • 708
  • 1
  • 6
  • 27
  • Does it work if you use `destdir` instead of `destdir/`? – Barmar Aug 12 '14 at 20:09
  • If I omit the "/", it writes into a file "destdir", overwriting it for each file i am trying to copy. The end result is that the contents of "destdir" is the text of the last file in the source files list. – Sam Coulter Aug 12 '14 at 20:11
  • 2
    I can't reproduce that. If the directory already exists, it copies into it whether I end with `/` or not. If the directory doesn't exist, and I end with `/`, it gets an error **No such file or directory**. – Barmar Aug 12 '14 at 20:14
  • Oh Boy. I'm running python3.4 on a 64bit Debian machine. Could this really be system specific? – Sam Coulter Aug 12 '14 at 20:15
  • I tried it in python 2.6 on OS X Snow Leopard. – Barmar Aug 12 '14 at 20:17
  • What is inside `srcdir`? – merlin2011 Aug 12 '14 at 20:34
  • 2
    I took a look at the source code for that version of Python, and it seems that `os.path.isdir` must be returning false for your destination directory to get the error you are getting. – merlin2011 Aug 12 '14 at 20:48
  • 1
    Tried your code snippet on 64-bit Debian (unstable) without any problems. It runs with Python 3.4 and 2.7 with and without trailing slash like expected. Perhaps try it with a ``srcdir`` and ``destdir``. In another context, I had a similar problem because the filename encoding was not uft-8. – Dietrich Aug 12 '14 at 21:41
  • Cannot reproduce it either. Your code looks good. – Juan Cespedes Aug 28 '14 at 16:28
  • Can you `tar` up a minimal pair of directories that cause this? – Veedrac Sep 29 '14 at 08:13
  • Could you post a solution to your problem or close/delete the question if it was a typographical error. – jb. Sep 29 '14 at 09:00

0 Answers0