-1

I have researched the similarly asked questions without prevail. I am trying to os.walk() a file tree copying a set of files to each directory. Individual files seem to copy ok (1st iteration atleast), but an error is thrown (IOError: [Errno 13] Permission denied: 'S:/NoahFolder\.images') while trying to copy a folder (.images) and its contents? I have full permissions to this folder (I believe).

What gives?

import os
import shutil
import glob

dir_src = r'S:/NoahFolder/.*'
dir_dst = r'E:/Easements/Lynn'
src_files = glob.glob(dir_src)
print src_files

for path,dirname,files in os.walk(dir_dst):
    for item in src_files:
        print path
        print item

        shutil.copy(item, path)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Noah Huntington
  • 409
  • 2
  • 5
  • 17
  • 1
    @CodyBouche these look like windows file paths, and telling someone to blindly `chmod 777` is bad advice. – Ryan Haining Sep 02 '15 at 18:57
  • @Noah create a smaller example. Can you use `shutil.copy` to copy a single item out of the problematic directory? – Ryan Haining Sep 02 '15 at 18:58
  • 2
    If you are getting a "Permission denied" error then it is reasonably safe to conclude that you don't "have full permissions to this folder". – msw Sep 02 '15 at 18:58

2 Answers2

1

shutil.copy will only copy files, not directories. Consider using shutil.copytree instead, that's what it was designed for.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • Your answer seems to hit close. I have tested using try/expect and can successfully place files outside of subfolder however no luck with subfolder? When run without comment the shutil.copytree call throws this error: [Error 183] Cannot create a file when that file already exists. `for path,dirname,files in os.walk(dir_dst): for item in src_files: try: shutil.copy(item, path) except: print item print path #shutil.copytree(item, path)` – Noah Huntington Sep 02 '15 at 19:56
  • Perhaps you are trying to copy files that `copytree` has already created for you, by continuing to walk down directories you have already processed? – holdenweb Sep 03 '15 at 04:13
1

This implementation of copytree seemed to get it done! Thanks for the input @ holdenweb

from distutils.dir_util import copy_tree

for path,dirname,files in os.walk(dir_dst):

    for item in src_files:
        try:
          shutil.copy(item, path)
        except:
            print item
            print path
            copy_tree(dir_src, path)
Noah Huntington
  • 409
  • 2
  • 5
  • 17