shutil.rmtree() clarification

PythonPython 2.7Shutil

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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'.

Solution 3 - Python

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.

Solution 4 - Python

**For Force deletion using rmtree command in Python:**

[user@severname DFI]$ python
Python 2.7.13 (default, Aug  4 2017, 17:56:03)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.rmtree('user/tester/noob')



But what if the file is not existing, it will throw below error:


>>> shutil.rmtree('user/tester/noob')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/shutil.py", line 239, in rmtree
    onerror(os.listdir, path, sys.exc_info())
  File "/usr/local/lib/python2.7/shutil.py", line 237, in rmtree
    names = os.listdir(path)
OSError: [Errno 2] No such file or directory: 'user/tester/noob'
>>>

**To fix this, use "ignore_errors=True" as below, this will delete the folder at the given if found or do nothing if not found**


>>> shutil.rmtree('user/tester/noob', ignore_errors=True)
>>>

Hope this helps people who are looking for force  folder deletion using rmtree.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIT NinjaView Question on Stackoverflow
Solution 1 - PythonRich WalshView Answer on Stackoverflow
Solution 2 - PythonkraseView Answer on Stackoverflow
Solution 3 - PythonclackeView Answer on Stackoverflow
Solution 4 - Pythonritu ameraView Answer on Stackoverflow