python shutil.copytree - ignore permissions
Asked Answered
T

4

7

Python's shutil.copytree is not very flexible; what is the simplest way to add support for ignoring permissions while copying in copytree (without having to re-write its implementation)?

Otherwise, copytree fails like this:

(…)”[Errno 45] Operation not supported: ‘/path/foo/bar’”
Training answered 20/8, 2009 at 0:51 Comment(1)
none of the solutions work for me, so i use shutil.copytree and make_writeable_recursiveAustraloid
A
3

You have shutil.py in your standard Python distribution (on Ubuntu, mine is under /usr/lib/python2.6 for instance; Windows might be C:\Python26\lib?). The copytree function is only 38 lines long (34 if you don't count comments), and the end of the docstring explicitly states:

XXX Consider this example code rather than the ultimate tool.

So the simplest way really would be to change/add a couple lines to copytree, or find another library, to be honest.

Abrego answered 20/8, 2009 at 0:58 Comment(2)
That's what I did before posting this question.Training
source: def copytree in shutil.pyAustraloid
S
5

Not thread-safe (or advisable in general) but OK for a throwaway script:

import shutil

_orig_copystat = shutil.copystat
shutil.copystat = lambda x, y: x

shutil.copytree(src, dst)

shutil.copystat = _orig_copystat
Solander answered 10/6, 2013 at 10:50 Comment(1)
fix: shutil.copystat = lambda *a, **k: None because the original function signature is copystat(src, dst, follow_symlinks=follow_symlinks)Australoid
A
3

You have shutil.py in your standard Python distribution (on Ubuntu, mine is under /usr/lib/python2.6 for instance; Windows might be C:\Python26\lib?). The copytree function is only 38 lines long (34 if you don't count comments), and the end of the docstring explicitly states:

XXX Consider this example code rather than the ultimate tool.

So the simplest way really would be to change/add a couple lines to copytree, or find another library, to be honest.

Abrego answered 20/8, 2009 at 0:58 Comment(2)
That's what I did before posting this question.Training
source: def copytree in shutil.pyAustraloid
A
1

In Python 3.2 and higher, there's now a built-in way to do this. shutil.copytree accepts a custom file copy function as an argument. You can use that to change it from the default file copy function (shutil.copy2) to one that doesn't copy permissions like shutil.copy:

shutil.copytree(src, dst, copy_function=shutil.copy)
Acre answered 2/11, 2020 at 23:24 Comment(2)
This will only work for files since directory permissions are not copied with the copy_function. They are mirrored explicitly with shutil.copystat() instead.Deckert
shutil.copy does copy file permissions (but not other metadata such as file creation time). To avoid copying file permissions, use shutil.copyfile as the copy function.Supertax
R
-1

Use subprocess to avoid permission issues. This worked for me:

import subprocess
command = f"cp -r {src_dir} {dst_dir}"
subprocess.run(command, shell=True)
Regeneration answered 8/10 at 12:3 Comment(1)
Why does this help? What would happen if src_dir = '; rm -rf'?Engstrom

© 2022 - 2024 — McMap. All rights reserved.