[Tutor] shutil.rmtree clarification

Steven D'Aprano steve at pearwood.info
Sat May 19 04:41:27 CEST 2012


Garry Willgoose wrote:

> I'm trying to remove a directory that has within it a directory tree structure and files and thought the following would work (the online docs seem to suggest it will remove the underlying directory tree and included files)
> 
> shutil.rmtree('test_directory')
> 
> but I got back an error (Python 2.7 on OSX)
> 
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/shutil.py", line 253, in rmtree
>     onerror(os.rmdir, path, sys.exc_info())
>   File "/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/shutil.py", line 251, in rmtree
>     os.rmdir(path)
> OSError: [Errno 66] Directory not empty: 'test_directory'
> 
> 
> which seems to suggest that the directory has to be empty to work ... sort of defeats the purpose of wanting to removing the tree in one command. What do people suggest short of calling os.system('rm -R test_directory').

No, the directory doesn't have to be empty. It's hard to say what's wrong, but 
  my guess would be that there's a file in test_directory that you don't have 
permission to remove.



-- 
Steven



More information about the Tutor mailing list