New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a "force" parameter to shutil.rmtree #66239
Comments
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). |
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). Not sure about Windows, though. |
Actually it does apply on unix: rdmurray@session: 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. |
No, it does not apply: here's what I've written: Having a file non-writable fits in those "some cases" where rm is |
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 bpo-19643, and that was more or less the conclusion. |
Doh. And I was even involved in the previous issue. Sorry for the noise. |
Exactly :-) |
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
$ python3 -c 'import shutil; shutil.rmtree("testdir")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
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 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: