Python

Python Delete File If Exists

There are many instances when the user wants to interact with the files on the system and delete a specific one if it exists. In Python, the “os” package is used to interact with files, and this package contains a different method that can help the user delete files on the system. These methods include the remove() and the unlink().

The following is the content of this post that will be covered:

Let’s start with the first method.

Method 1: Using the remove() Method to Delete a File

The remove() method can be used to delete a specific file from the system by providing either its relative path or its specific path. However, this method will cause the program to run into an error and crash if the file specified in its argument doesn’t exist. To avoid this, the user needs to wrap this command with a “try-except” statement.

To demonstrate the working of this method, refer to the code provided below:

import os

try:
    os.remove("writeMe.txt")
    print("The File has been deleted.")
except:
    print("File Doesn't Exist in Specified Path")

The goal of this goal is to delete a file named “writeMe.txt” which is in the same folder as the program:

When this program is executed, the following prompt is displayed on the terminal:

The prompt tells the user that the file has been deleted, which can be confirmed by looking for to any file explorer:

However, let’s rerun the code to observe the behavior of the code when the file doesn’t exist:

As you can see in the output, instead of crashing, the program prompts the user that the file doesn’t exist and thus the remove() method cannot be executed.

Method 2: Using the unlink() Method to Delete a File

The unlink() method works almost identically with the remove() method. It is also used to delete a file specified by its path in the argument of the unlink() method. To demonstrate the working of the unlink() method, take the following code snippet:

import os

try:
    os.unlink("readMe.txt")
    print("The File has been deleted.")
except:
    print("File Doesn't Exist in Specified Path")

This code snippet will delete the “readMe.txt” file from the relative directory:

When the code is executed, it will produce the following outcome on the terminal:

You can confirm this deletion by using any file explorer:

The file has been successfully removed from the system with the help of the unlink() method.

Conclusion

The user can use the remove() method and the unlink() method of the “os” package to delete a specific file only if it exists in the system. Both of these methods essentially have almost similar working. Both of these methods take in the relative or the specific path of the file to be deleted, and if the file is found, they delete it. If the file is not found, the program runs into an error. To avoid this crash, simply use the try-except error handling statements.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.