1

At least on windows, shutil.move a folder containing readonly files to another drive will fail. It fails because move is implemented with a copy followed by a rmtree. In the end, it's the rmtree trying to delete non writable files.

Currently I work around it by first setting the stat.S_IWUSER for all (nested) files, but now I should still restore the original stat afterwards:

def make_tree_writable(source_dir):
    for root, dirs, files in os.walk(source_dir):
        for name in files:
            make_writable(path.join(root, name))    

def make_writable(path_):
    os.chmod(path_, stat.S_IWUSR)

def movetree_workaround(source_dir, target_dir):
    make_tree_writable(source_dir)
    shutil.move(source_dir, target_dir)

So I wonder: is this the way? Is there a shutil2 in the making that I could use? Can I be of any help there?

xtofl
  • 40,723
  • 12
  • 105
  • 192

1 Answers1

2

You can do that in two steps: first, use shutil.copytree() to copy the full directory and file structure with appropriate permissions. Then you can change permissions of the source to make sure you have rights to delete stuff, and use shutil.rmtree() to remove the old source.

VBB
  • 1,305
  • 7
  • 17