{"id":5669,"date":"2021-05-16T10:44:19","date_gmt":"2021-05-16T05:14:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=5669"},"modified":"2021-11-22T18:40:51","modified_gmt":"2021-11-22T13:10:51","slug":"python-how-to-delete-a-directory-recursively-using-shutil-rmtree","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-delete-a-directory-recursively-using-shutil-rmtree\/","title":{"rendered":"Python : How to delete a directory recursively using shutil.rmtree()"},"content":{"rendered":"
In this article, we will discuss about how we can delete an empty directory and also all contents of a directory including sub directories using shutil.rmtree().<\/p>\n
The os module of python provide a function i.e. The shutil module of python provides a function i.e. Here in this case if found, all contents of directory In previous scenario if we failed to delete any file, then we can’t delete further required files in the directory. Now, we can ignore the errors by passing It will skip the file which raised the exception and then going forward it can delete all of the remaining files.<\/p>\n Let there be a scenario where we can’t delete a file in log directory, then we can use the following syntax so that this exception can be ignored:<\/p>\n Instead of ignoring errors, there may be a case where we have to handle the errors.<\/p>\n In The callable function passed must be callable like:<\/p>\n where,<\/p>\n Now, if any exception is raised while deleting a file, then callback will handle the error. Then, Now let a case where we want to delete all the contents of a directory In the callback if it accesses issue, then we will change the file permission and then call calling function i.e. <\/p>\n <\/p>\n \u00a0<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":" How to delete a directory recursively using shutil.rmtree() in Python ? In this article, we will discuss about how we can delete an empty directory and also all contents of a directory including sub directories using shutil.rmtree(). Delete an empty directory using os.rmdir() : The os module of python provide a function i.e. os.rmdir(pathOfDir) which …<\/p>\nos.rmdir(pathOfDir)<\/code> which can delete an empty directory. It can also give rise to certain errors in some scenarios like:<\/p>\n
\n
#Program :\r\n\r\nimport os\r\n\r\n# Deleting a empty directory using function os.rmdir() and handle the exceptions\r\ntry:\r\n os.rmdir('\/somedirec\/log2')\r\n\r\nexcept:\r\n print('Not found directory')<\/pre>\n
Output :\r\nNot found directory<\/pre>\n
Delete all files in a directory & sub-directories recursively using shutil.rmtree() :<\/h3>\n
shutil.rmtree()<\/code> to delete all the contents present in a directory.<\/p>\n
\u00a0Syntax : shutil.rmtree(path, ignore_errors=False, onerror=None)<\/pre>\n
import shutil\r\n\r\ndirPath = '\/somedirec\/logs5\/';\r\n\r\n# trying to delete all contents of a directory and handle exceptions\r\ntry:\r\n shutil.rmtree(dirPath)\r\n\r\nexcept:\r\n print('Not found directory')<\/pre>\n
Output :\r\nNot found directory<\/pre>\n
'\/somedir\/logs\/'<\/code> will be deleted. If any of the files in directory has read only attributes then user can’t delete the file and exception i.e. PermissionError will be raised therefore we can’t delete any remaining required files.<\/p>\n
shutil.rmtree() & ignore_errors :<\/h3>\n
ignore_errors=True<\/code> in
shutil.rmtree()<\/code>.<\/p>\n
shutil.rmtree(dirPath, ignore_errors=True)<\/pre>\n
Passing callbacks in shutil.rmtree() with onerror :<\/h3>\n
Syntax : <\/u>\u00a0shutil.rmtree(path, ignore_errors=False, onerror=None)<\/pre>\n
onerror<\/code> parameter, callback function can be called to handle the errors<\/p>\n
shutil.rmtree(dirPath, onerror=handleError)<\/pre>\n
def handleError(func, path, exc_info):\r\n\u00a0 \u00a0pass<\/pre>\n
\n
shutil.rmtree()<\/code> can continue deleting remaining files.<\/p>\n
'\/somediec\/logs'<\/code>, and there is somewhere a file in logs directory which shows permission issues, and we can’t delete all. So callback will be passed in oneerror parameter of that file.<\/p>\n
rmtree()<\/code> with path of file. Then the file will be deleted and
rmtree()<\/code> will further delete remaining contents in the directory.<\/p>\n
import os\r\nimport shutil\r\nimport stat\r\n\r\n#Error handling function will try to change permission and call calling function again\r\n\r\ndef handleError(func, path, exc_info):\r\n print('Handling Error for file ' , path)\r\n print(exc_info)\r\n\r\n# check that if the file is accessing the issue\r\nif not os.access(path, os.W_OK):\r\n print('helloWorld')\r\n # trying to change the permission of file\r\n os.chmod(path, stat.S_IWUSR)\r\n # Tring to call the calling function again\r\n func(path)<\/pre>\n