126 of 151 menu

The rmtree method of the shutil module

The rmtree method of the shutil module deletes a folder with all the files and folders it contains. In the first parameter of the method, we specify the path to the folder. In the second optional parameter, we specify ignore_errors. If its value is True, then errors that occur as a result of unsuccessful deletion will be ignored. In the third optional parameter, we specify oneerror. If its value is False, then the errors will be processed by calling the specified handler. If the deletion is successful, the method returns None.

Syntax

import shutil shutil.rmtree(path to folder, [ignore_errors], [oneerror])

Example

Let's delete the dir folder with all its contents:

import shutil print(shutil.rmtree('dir2'))

The result of the executed code:

None

See also

  • method move of module shutil,
    which recursively moves a file or directory
  • method copytree of module shutil,
    which recursively copies a folder
  • method remove of module os,
    which deletes the file
  • method makedirs of module os,
    which creates folders
  • method rename of module os,
    which renames a file and folder
  • method scandir of module os,
    which iterates through the files in a folder
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline