Python provides a great communication channel with OS, hence it can delete, move, create files in the system. In this Python tutorial, we will learn about some methods using which we remove/delete a folder that is not empty.
Table Of Contents
Here is the file structure:
Method 1 : Using shutil module
First method that we will be using to remove a folder that is not empty is in the
shutil module which comes pre-installed with Python programming language. It is mostly used for automation of file handling operations like: copying, deletion and creation. Here we will use
shutil.rmtree() function which deletes the entire directory. This cannot delete read-only folders, hence it will throw an error in that case.
See the example code below:
# import shutil import shutil # deleting directory using the rmtree. shutil.rmtree(path='../python',ignore_errors=True)
In this example, the
shutil.rmtreee() function with arguments –
path to the directory and
ignore_errors=True has been used to remove/delete a folder that is not empty.
ignore_errors=True has been used because this method can not delete read only files.
Method 2 : Using OS module
Another method we can use to remove/delete a folder that is not empty is
OS module comes pre-installed with python programming language and it is the one of the most used module for interaction with system and os. The
os.walk() function traverse from top to bottom or from bottom to top of the provided path and returns three tuples which are : root, dirs and files. Also, we will use
os.remove() to remove the files from the system.
See the example code below
import os for (roots,dirs,files) in os.walk('../python',topdown=True): print(roots) print(dirs) print(files)
../python ['sd1', 'sd2']  ../python\sd1  ['file1.txt'] ../python\sd2  ['file2.txt']
Above was directory structure generated by os.walk() function.
Now the code to remove/delete a folder that is not empty
import os # os.walk will iterate from top to bottom of the root directory. for (roots,dirs,files) in os.walk('../python',topdown=True): # iterating through all the files. for name in files: # this will delete all the directories. os.remove(os.path.join(roots, name))
In the code above, you can see os.walk() function and os.remove() function has been used to remove/delete a folder that is not empty.
So In this Python tutorial, we learned How to remove/delete a folder that is not empty. For this we have two methods of two different modules. First is rmtree() function of shutil module and second is remove() function of os module. We also learned about os.walk() who iterates from top to bottom or bottom to top as passed in the argument. Both the methods can be useful, but the
os module is the best to use because it is the most preferred module of file manipulation and by iterating over the directory using the
os.walk() function you can easily understand the directory also this function is very easy to understand.
Make sure to read and write code examples mentioned above, in order to have a better understanding. The path should be written very carefully specially with the root directory. path to the Python Interpreter should be in your working directory and then you can write the correct path of the folder to be deleted. Thanks.
Thanks for reading.