As a start, have you done this:
Before the rmtree command, find out which directory you end up in after all of your commands, and they figure out whether you have access to the directory you are trying to delete from there. -- https://mail.python.org/mailman/listinfo/python-list