Remove directory tree without following symlinks

Albert-Jan Roskam sjeik_appie at hotmail.com
Sat Apr 23 05:34:12 EDT 2016



> From: eryksun at gmail.com
> Date: Fri, 22 Apr 2016 13:28:01 -0500
> Subject: Re: Remove directory tree without following symlinks
> To: python-list at python.org
> 
> On Fri, Apr 22, 2016 at 12:39 PM, Albert-Jan Roskam
> <sjeik_appie at hotmail.com> wrote:
> > FYI, Just today I found out that shutil.rmtree raises a WindowsError if the dir is read-
> > only (or its contents). Using 'ignore_errors', won't help. Sure, no error is raised, but the
> > dir is not deleted either! A 'force' option would be a nice improvement.
> 
> Use the onerror handler to call os.chmod(path, stat.S_IWRITE). For
> example, see pip's rmtree_errorhandler:
> 
> https://github.com/pypa/pip/blob/8.1.1/pip/utils/__init__.py#L105

Thanks, that looks useful indeed. I thought about os.chmod, but with os.walk. That seemed expensive. So I used subprocess.call('rmdir "%s" /s /q' % dirname). That's Windows only, of course, but aside of that, is using subprocess less preferable?Fun fact: I used it to remove .svn dirs, just like what is mentioned in the pip comments :-)

 		 	   		  


More information about the Python-list mailing list