Example 1: Using the Python Os.Remove() Method to Delete the Files from the System
In this example, we use the “os.remove()” method to delete the files from the system. By adding the file’s path in the “remove()” method, we may delete the files. Importing the “os” library is necessary to remove the file from the system. A modularity operating system divides its various activities into discrete tasks, each with its user interface. In contrast, a traditional monolithic operating system has a single user-defined version and operates in an all-or-nothing way. The tools for generating and wiping directories, documents, and the information that they hold as well as for modifying and accessing the directory structure are all available in the Python OS package.
Let’s now just begin the procedure to remove the file from the system. We start the procedure by creating two text files on the desktop and giving them the “record” and “data” names as can be seen in the following. We now begin the code to delete these two files after creating them:
The first file which has the “record” name is deleted in the first section. Let’s get started by importing the Python “os” module. The operating system extensions are not required for Python; you may just import the “os” library. Then, we add the file name that we want to remove from the system. In this case, we remove the “record.txt” file because it is a text document. This is why the “.txt” is included in the file name. This file name is kept in the “file_name” variable that we made.
Then, in the following step, we add the location of the file since it is already saved on the desktop. Go to the document, click it, and select “properties” to do this. From there, we copy the location and paste it, which is “Users/AqsaYasin/Desktop”. This shows the “AqsaYasin” user name and the desktop as the document’s storage place. This file location is kept in the “file_location” variable. To properly establish the path of the file that we want to delete, we now join the path using the “os.path.join()” function and supply the “file_name” and “file_location” arguments to it. We store this information in the newly generated variable, “path”.
Since the file’s path is kept in the “path” variable, we call the “remove()” method with “os” and pass it to the variable. The “print()” method is then used to print the statement, “The file is deleted”.
To make it simple for you to copy and paste the code on your compiler, we also provide you with a code screenshot and paste the code directly from the compiler.
Code:
file_name = 'record.txt'
file_location = r"C:/Users/AqsaYasin/Desktop"
path = os.path.join(file_location, file_name)
os.remove(path)
print("The file is deleted.")
The message “The file is deleted” is displayed in the output:
Now that the code is executed, the record file is deleted because there were previously two files named “record” and “data” on the desktop. From there, we delete the record file. Now, we are left with the “data” file.
The second file in this example, the “data”, is now deleted in the following section. We follow the identical steps as in the previous section to do this, with the exception that the file name is the “data.txt” in this case. Then, after adding the location, we combine the “file name” and “file location” by passing them to the “os.path.join()” method. This method combines the file name and the location, creating the path for the file that we want to delete which is “User/AqsaYasin/Desktop”. To remove the file, we then call the “remove()” method while providing a “path” argument. The statement “The file is deleted” is then displayed using the “print()” method.
Code:
file_name = 'data.txt'
file_location = r"C:/Users/AqsaYasin/Desktop"
path = os.path.join(file_location, file_name)
os.remove(path)
print("The file is deleted.")
The “data” file is successfully wiped from the desktop when the script is executed. And the message “The file is deleted” is displayed:
Example 2: Utilizing the If-Else Condition and the Os.Remove() Method to Delete the File
In this example, we eliminate a document from the desktop using the “remove()” method. Then, we apply the “If” condition inside the “remove()” method. If the file is on the system, it is deleted and the statement “The file is removed” is printed. If it’s not, it says “The file is not present in the system”.
Let’s begin putting the code into practice by first importing the Python “os” module as “o”. Then, we paste the path “Users/AqsaYasin/Desktop/A yasin.txt” into the code. This path is kept in the “P” variable that we made. As seen in the following screenshot, this file is present on our system’s desktop.
Then, we use “exists()” with “o.path” to apply an “if” condition. Inside that, we call the “o.remove()” method and pass the path using the “P” variable because the file’s path is saved in this variable. If the path is precise or the file is present on the system desktop, the first statement is printed. If not, the second statement is printed.
Code:
P = r'C:/Users/AqsaYasin/Desktop/A_Yasin.txt'
if o.path.exists(P):
o.remove(P)
print("The file is removed")
else:
print("The file is not present in the system")
When we press the run button, the result is presented. It shows the if statement which reads, “The file is removed”, because the file is already present in the system and is deleted when the “remove()” method is applied.
Now, in its subsequent section, we print the else statement by including the path of a file that is not currently on the system. The “Users/AqsaYasin/Desktop/subject.txt” path is used in this section. Since the file subject.txt is missing from the system, the else statement is printed when we use the “o.remove()” method.
Code:
P = r'C:/Users/AqsaYasin/Desktop/subjects.txt'
if o.path.exists(P):
o.remove(P)
print("The file is removed")
else:
print("The file is not present in the system")
Since this file is not on our system, the else statement “The file is not present in the system” is displayed. We place the file name in the path at random to see if the “remove()” function works under the “If” condition or not.
To remove the file from the system, we use the “remove()” method inside the “try” and “except” blocks in the third section of this example. You can see in the following image that there is a file named “Python” on your desktop. We’re going to delete it using the “os.remove()” method.
Now, let’s begin to put the code into action. First, import the Python “OS” module as “O”. Then, call the “try” block which allows you to check a piece of code for errors. The unless block helps you fix the error. The “remove()” method is called inside the “try” block with the “User/AqsaYasin/Desktop/python.txt” path which is passed in its parentheses because the file’s name is “Python”. Next, the “print()” function is called with the statement “File is deleted” which is passed in. And the “except” block is used. Inside of it, we call the “print()” function with the “file is not present” statement.
Code:
try:
O.remove(r'C:/Users/AqsaYasin/Desktop/python.txt')
print("File is deleted")
except:
print("file is not present")
Now that the file is erased from the system, the message “File is deleted” is displayed, indicating that the code is successfully executed.
Conclusion
In this article, we covered how to delete the files from the system in Python. To do so, we utilized the “os.remove()” method. The “remove()” method is invoked with the particular path of the file that we want to delete from the system as an argument. In this article, we provided two examples. In the first example, two files were deleted. In the second example, the “remove()” function is called inside of an “if” condition. In the second portion of the second example, we used this method inside of a try-and-except block to delete the file.