In this article we will discuss how to delete an empty directory and also all contents of directory recursively i.e including contents of its sub directories.

Delete an empty directory using os.rmdir()

Python’s os module provide a function to delete an empty directory i.e.

Path of directory can be relative or absolute. It will delete the empty folder at given path.
It can also raise errors in following scenarios,

  • If directory is not empty then it will cause OSError i.e.
    • OSError: [WinError 145] The directory is not empty:
  • If given directory path is not pointing to a directory, then this error will be raised,
    • NotADirectoryError: [WinError 267] The directory name is invalid:
  • If there is no directory at given path then this error will be raised,
    • FileNotFoundError: [WinError 2] The system cannot find the file specified:

Let’s use this to delete an empty directory,

Delete all files in a directory & sub-directories recursively using shutil.rmtree()

Python’s shutil module provides a function to delete all the contents of a directory i.e.

It accepts 3 arguments ignore_errors, onerror and path.

path argument should be a path of the directory to be deleted. We we will discuss other arguments very soon.

Module required,

Let’s use this to delete all the contents of a directory i.e.

It will delete all the contents of directory’/somedir/logs/’

But if any of the file in directory has read only attributes i.e. user can not delete that file, then it will raise an exception i.e.
PermissionError: [WinError 5] Access is denied:

Also it will not delete the remaining files. To handle this kind of scenario let’s use other argument ignore_errors.

shutil.rmtree() & ignore_errors

by passing ignore_errors=True in shultil.rmtree() we can ignore the errors encountered. It will go forward with deleting all the files and skip the files which raise exceptions while deleting.

Suppose we have a file in log directory that can not be deleted due to permission issues. So,

will remove all the other files from ‘/somedir/logs’ directory except the file with permission issues. Also, it will not raise any error.

But this might not be the case always, we might want to handle errors instead of ignoring them. For that we have other argument of shutil.rmtree() i.e. onerror.

Passing callbacks in shutil.rmtree() with onerror

In onerror parameter we can pass a callback function to handle errors i.e.

callback function passed in onerror must be a callable like this,

It should accepts three parameters:

  • function
    • function which raised the exception
  • path
    • path name passed which raised the exception while removal
  • excinfo
    • exception information returned by sys.exc_info()

If any exception occurs while deleting a file in rmtree() and onerror callback is provided. Then callback will be called to handle the error. Afterwards shutil.rmtree() will continue deleting other files.

Now suppose we want to delete all the contents of directory ‘/somedir/logs’ . But we have a file in logs directory that can not be deleted due to permission issues. Let’s pass a callback to handle the error

Now while deleting all the files in given directory, as soon as rmtree() encounters a file that can not be deleted, it calls the callback passed in onerror parameter for that file.
In this callback we will check if it’s am access issue then we will change the file permission and then call called function func i.e. rmtree() with the path of file. t will eventually delete the file. Then rmtree() will continue deleting other files in the directory.

Complete example is as follows,


Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.