I have read the documentation for this function, however, I dont think I understand it properly. If anyone can tell me what I'm missing, or if I am correct, it would be a great help. Here is my understanding:
using the shutil.rmtree(path)
function, it will delete only the directory specified, not the entire path. IE:
shutil.rmtree('user/tester/noob')
using this, it would only delete the 'noob' directory correct? not the complete path?
There is some misunderstanding here.
Imagine a tree like this:
If you want to delete
user
, just doshutil.rmtree('user')
. This will also deleteuser/tester
anduser/tester/noob
as they are insideuser
. However, it will also deleteuser/developer
anduser/developer/guru
, as they are also insideuser
.If
rmtree('user/tester/noob')
would deleteuser
andtester
, how do you meanuser/developer
would exist ifuser
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 firstnoob
, then it would try to removetester
, which is ok because it's empty, but it would stop atuser
and leave it alone, because it containsdeveloper
, which we do not want to delete.This will definitely only delete the last directory in the specified path. Just try it out:
...will only remove
'bar'
.If noob is a directory, the
shutil.rmtree()
function will deletenoob
and all files and subdirectories below it. That is,noob
is the root of the tree to be removed.