shutil.rmtree() clarification
If noob is a directory, the shutil.rmtree()
function will delete noob
and all files and subdirectories below it. That is, noob
is the root of the tree to be removed.
This will definitely only delete the last directory in the specified path. Just try it out:
mkdir -p foo/bar
python
import shutil
shutil.rmtree('foo/bar')
...will only remove 'bar'
.
There is some misunderstanding here.
Imagine a tree like this:
- user
- tester
- noob
- developer
- guru
If you want to delete user
, just do shutil.rmtree('user')
. This will also delete user/tester
and user/tester/noob
as they are inside user
. However, it will also delete user/developer
and user/developer/guru
, as they are also inside user
.
If rmtree('user/tester/noob')
would delete user
and tester
, how do you mean user/developer
would exist if user
is gone?
Or do you mean something like http://docs.python.org/2/library/os.html#os.removedirs ?
It tries to remove the parent of each removed directory until it fails because the directory is not empty. So in my example tree, os.removedirs('user/tester/noob')
would remove first noob
, then it would try to remove tester
, which is ok because it's empty, but it would stop at user
and leave it alone, because it contains developer
, which we do not want to delete.