What does shutil.rmtree(d, ignore_errors=True) do?

I was working with shutil module. My file structure looked like this projects/backend/--3-5 repos-- .

In one of the repo's views.py I used

for d in destDirs: 
    shutil.rmtree(d, ignore_errors=True)

destDirs = "projects/backend/repo1/static/file1

After that my whole file system got deleted.

After running the API that had that code my whole file system got deleted.

since destDirs is a string, it will enumerate over all characters of the string, and thus call:

shutil.rmtree('p', ignore_errors=True)
shutil.rmtree('r', ignore_errors=True)
shutil.rmtree('o', ignore_errors=True)
shutil.rmtree('j', ignore_errors=True)
shutil.rmtree('e', ignore_errors=True)
shutil.rmtree('c', ignore_errors=True)
shutil.rmtree('t', ignore_errors=True)
shutil.rmtree('s', ignore_errors=True)
shutil.rmtree('/', ignore_errors=True)  # root
# …

so yes that will remove the entire file system, if it can be called with / (since that is the root of the filesystem).

you thus use:

shutil.rmtree(destDirs, ignore_errors=True)

or you make destDirs a list of strings, which the name suggests it is.

Вернуться на верх