Introduction
In this article, I'm going to explain how we can perform the deletion with python and know about the required methods and libraries, that are being used in our code. First, we are going to understand the possible conditions for deletion. Next, we see the requirements to perform the deletion. Last we see some examples to perform deletion for each condition. So let's begin,
Conditions For Deletion
There could be 3 conditions when we go for deletion
- Delete file
- Delete empty folder
- Delete the folder with files
Python is a very user-friendly language, we can perform these 3 tasks very easily by using python libraries, so let's focus on these libraries and methods.
Requirements to perform deletion in python
Here we have two libraries that are being used to delete a file, delete an empty folder, and delete a folder with files. These libraries are written below
- os module
- shutil module
OS module
OS module is used to interact with the Operating system, we can get a current working directory by using os.getcwd() and create directories by using os.mkdir(), and many other functionalities, we just need to import the OS module in our program. We need this module when we want to delete a single file or an empty folder.
Shutil module
According to the python Official, documentation - The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided that support file copying and removal. For operations on individual files, we need this module when we want to delete a folder with all the containing files.
Let's have an example to delete a file
First, we create a test folder and a text file, then we delete that file. We can create files and directories using two ways first manually and second, we can create using code. Here I'm using the second way.
Program to create an empty folder. I'm using Python IDLE 3.11. You can use, which is installed on your machine.
# Python program to Create Directory using os.mkdir() method
# importing os module
import os
# Directory
directory = "CSharpDir"
# Location path
Location = "D:/"
# Path
path = os.path.join(Location, directory)
# Create the directory in given location
os.mkdir(path)
print("Directory '% s' created" % directory)
Output
![]()
Figure-Directory Created
If we check in the following location the directory is created. Let's have a look
![]()
Figure-Dir Created
First, we have added the OS module by using the import. Next stored the directory location and directory name and combine them together by using os.path.join() method and in the last, we have passed this location to the os.mkdir() method. This method has created the directory based on the given credentials.
Now let's create a file using code in this folder
# importing os module
import os
f = open("D:/CSharpDir/myfile.txt", "x")
print(" file created")
Output
![]()
Figure-File-created
Here I have used the x command it will create a file if no file exists with the same name, otherwise it will throw an error.
Now we have created our file and folder, now let's Delete these files and folded with python
Delete a file using the OS module
# importing os module
import os
myfile="D:/CSharpDir/myfile.txt"
os.remove(myfile)
Output
![]()
Figure-File deleted
Here we have used os.remove() method and passed the location with the file name and it will delete the file directly.
Delete an Empty folder using the OS module
# importing os module
import os
myfolder="D:/CSharpDir"
os.rmdir(myfolder)
print("Folder Deleted")
Output
![]()
Figure-Folder Deleted
Here we have used os.rmdir() method and passed the path of the folder. This will delete the folder if it's an empty folder.
Let's try to delete a folder that contains some files with the same method. Now I'm going to create a new folder with files by using the same code
![]()
Figure-File with folder
I have created a folder with the name pythonNoted and also a file with the name Note1. Now let's try to delete this folder with the same approach
![]()
Figure-Dir-not empty
That's exactly what I was expecting, We got an error that the directory is not empty, don't worry guys we have another solution for this situation. But the reason for this error is that os.rmdir() is not capable of deleting the folder that contains any file.
Now let's go for our Shutil module. This module provides a method shutil.rmtree(). This method is capable of removing or deleting all the trees containing files in that folder. Let's have an example
# importing shutil module
import shutil
myfolder="D:/PythonNoted"
shutil.rmtree(myfolder)
print("Folder Deleted")
Output
![]()
Figure-Deleted Folder with file
Here we have used shutil.rmtree() method and passed the location of the folder and it will delete the folder from the system.
Conclusion
Python is a powerful programming language. We can easily perform these tasks by using python libraries. In this article, we have created folders and files and deleted them through code.