How to Delete Folder and Files Recursively in Python?
In this post, we will learn how to delete folder and files recursively in python. it's simple example of python remove files recursively. This tutorial will give you simple example of python delete all files recursively. In this article, we will implement a python remove all files recursively. Let's see bellow example python delete files recursively.
In this example, we'll utilise the functions exists() and rmtree() of "os" and the shutil library to recursively delete every folder and file. So let's try the example below.
exists() will check file is exists or not.
rmtree() will delete directory with all files recursively.
So let's see bellow example:
I have created following files on files folder and remove all files and folders:
files/test.txt files/demo/test.txt files/test/test.txtmain.py
import os import shutil folderPath = 'files/'; # Check Folder is exists or Not if os.path.exists(folderPath): # Delete Folder code shutil.rmtree(folderPath) print("The folder has been deleted successfully!") else: print("Can not delete the folder as it doesn't exists")Output
The folder has been deleted successfully!
It will help you....
Happy Pythonic Coding!