shutil.rmtree raises "OSError: [Errno 39] Directory not empty" exception

Sean DiZazzo half.italian at gmail.com
Sun Jul 12 20:03:32 EDT 2009


On Jul 10, 5:10 pm, Tim Chase <python.l... at tim.thechases.com> wrote:
> >      shutil.rmtree(filename)
> >    File "/usr/lib64/python2.5/shutil.py", line 178, in rmtree
> >      onerror(os.rmdir, path, sys.exc_info())
> >    File "/usr/lib64/python2.5/shutil.py", line 176, in rmtree
> >      os.rmdir(path)
> > OSError: [Errno 39] Directory not empty: /path/to/my/dir
>
> > According to the documentation, shutil.rmtree should not care about  
> > directory being not empty.
>
> This sounds suspiciously like a permission issue.  rmtree()
> *should* walk the tree removing items *if it can*.  If a file
> can't be deleted, it treats it as an error.  rmtree() takes
> parameters for ignore_errors and an onerror callback function, so
> you can catch these error conditions.
>
> -tkc

This one took me a long time to find a solution for.  Check this page,
and see comment #3: http://code.activestate.com/recipes/193736/

I guess if the file is marked as "Read Only" or "Archive", or
whatever, it cannot be deleted with shutil.rmtree()

The key:  win32api.SetFileAttributes(path,
win32con.FILE_ATTRIBUTE_NORMAL)

It will work!

~Sean



More information about the Python-list mailing list