msg223685 - (view) |
Author: Paul Moore (paul.moore) *  |
Date: 2014-07-22 19:54 |
It would be useful for shutil.rmtree to have a "force" argument that overrode read-only permission issues, essentially replicating the behaviour of the -f flag in rm -rf (Unix) and the -force parameter of del (Windows Powershell). It's possible to use the onerror callback to implement this, but it's tricky to get right in a cross-platform manner. See http://stackoverflow.com/questions/2656322, which recommends def onerror(func, path, exc_info): if not os.access(path, os.W_OK): os.chmod(path, stat.S_IWUSR) func(path) else: raise and http://stackoverflow.com/questions/1889597 which recommends def remove_readonly(func, path, excinfo): os.chmod(path, stat.S_IWRITE) func(path) It's not clear whether either of these is portable, though (the former looks to me like it's Unix-specific and the latter like it's for Windows, but I'm not sure). Having the functionality available in the standard library function directly avoids having people write tricky and potentially buggy code for what is a pretty common situation. (In particular, this comes up a lot in code that deletes git checkouts on Windows, where git makes parts of the .git directory readonly). |
|
|
msg223686 - (view) |
Author: Charles-François Natali (neologix) *  |
Date: 2014-07-22 20:02 |
On Unix at least, this doesn't apply: rm -f doesn't mean "ignore permissions", but but rather don't ask confirmation which the rm commands asks in some cases (empty file, directory, etc). Ans the code posted wouldn't work, since the permission to remove a file applies to to the file, but to the parent directory (must have write permission). And we certainly don't want to change the directory permission anyway. Not sure about Windows, though. |
|
|
msg223687 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2014-07-22 20:12 |
Actually it does apply on unix: rdmurray@session:~/tmp>ls -ld foo drwxr-x--- 2 rdmurray rdmurray 4096 Jul 22 16:09 foo rdmurray@session:~/tmp>ls -l foo total 0 -r--r----- 1 rdmurray rdmurray 0 Jul 22 16:09 bar rdmurray@session:~/tmp>rm -r foo rm: remove write-protected regular empty file ‘foo/bar’? If I say yes it will remove it, since I have write perms on the directory. rm -rf will not prompt and will delete it. I believe the situation is analogous on Windows, but I"m not sure. |
|
|
msg223689 - (view) |
Author: Charles-François Natali (neologix) *  |
Date: 2014-07-22 20:20 |
> Actually it does apply on unix: No, it does not apply: here's what I've written: """ rm -f doesn't mean "ignore permissions", but but rather don't ask confirmation which the rm commands asks in some cases (empty file, directory, etc). """ Having a file non-writable fits in those "some cases" where rm is extra careful and asks confirmation.: calling shutil.rmtree() on your example will succeed, so it does not apply on Unix. |
|
|
msg223694 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2014-07-22 20:36 |
In other words, on unix shutil.rmtree is *already* 'rm -rf'. This then argues that it *not* deleting read only files on Windows is a bug, albeit one we may not be able to fix for backward compatibility reasons. Ah, and now my memory is jogged. This is a duplicate of issue 19643, and that was more or less the conclusion. |
|
|
msg223702 - (view) |
Author: Paul Moore (paul.moore) *  |
Date: 2014-07-22 21:42 |
Doh. And I was even involved in the previous issue. Sorry for the noise. |
|
|
msg223703 - (view) |
Author: Charles-François Natali (neologix) *  |
Date: 2014-07-22 21:49 |
> In other words, on unix shutil.rmtree is *already* 'rm -rf'. Exactly :-) |
|
|
msg265846 - (view) |
Author: Nirbheek Chauhan (Nirbheek Chauhan) |
Date: 2016-05-19 05:01 |
> In other words, on unix shutil.rmtree is *already* 'rm -rf'. This is not true. See: $ mkdir testdir && chmod 200 testdir && ls -lhd testdir d-w------- 2 nirbheek nirbheek 4.0K May 19 10:21 testdir `rm -rf` works fine on this. But shutil.rmtree borks: $ python3 -c 'import shutil; shutil.rmtree("testdir")' Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.5/shutil.py", line 470, in rmtree onerror(os.lstat, path, sys.exc_info()) File "/usr/lib64/python3.5/shutil.py", line 468, in rmtree fd = os.open(path, os.O_RDONLY) PermissionError: [Errno 13] Permission denied: 'testdir' The -f option to `rm` ensures that it tries its hardest to resolve permissions problems and does not error out if it can't resolve them either. The latter is available via 'ignore_errors', but the former is a missing feature. A shutil.rmtree flag that 'resolves permissions' would be useful on all platforms. Not just Windows. |
|
|