HomeLinuxPython Delete File If Exists

Python Delete File If Exists


There are numerous cases when the person desires to work together with the recordsdata on the system and delete a selected one if it exists. In Python, the “os” bundle is used to work together with recordsdata, and this bundle accommodates a distinct technique that may assist the person delete recordsdata on the system. These strategies embrace the take away() and the unlink().

The next is the content material of this put up that might be coated:

Let’s begin with the primary technique.

Technique 1: Utilizing the take away() Technique to Delete a File

The take away() technique can be utilized to delete a selected file from the system by offering both its relative path or its particular path. Nonetheless, this technique will trigger this system to run into an error and crash if the file laid out in its argument doesn’t exist. To keep away from this, the person must wrap this command with a “try-except” assertion.

To show the working of this technique, check with the code offered under:

import os

attempt:
    os.take away(“writeMe.txt”)
    print(“The File has been deleted.”)
besides:
    print(“File Would not Exist in Specified Path”)

The purpose of this purpose is to delete a file named “writeMe.txt” which is in the identical folder as this system:

When this program is executed, the next immediate is displayed on the terminal:

The immediate tells the person that the file has been deleted, which may be confirmed by in search of to any file explorer:

Nonetheless, let’s rerun the code to look at the conduct of the code when the file doesn’t exist:

As you may see within the output, as a substitute of crashing, this system prompts the person that the file doesn’t exist and thus the take away() technique can’t be executed.

Technique 2: Utilizing the unlink() Technique to Delete a File

The unlink() technique works virtually identically with the take away() technique. It is usually used to delete a file specified by its path within the argument of the unlink() technique. To show the working of the unlink() technique, take the next code snippet:

import os

attempt:
    os.unlink(“readMe.txt”)
    print(“The File has been deleted.”)
besides:
    print(“File Would not Exist in Specified Path”)

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

When the code is executed, it can produce the next consequence on the terminal:

You may verify this deletion through the use of any file explorer:

The file has been efficiently faraway from the system with the assistance of the unlink() technique.

Conclusion

The person can use the take away() technique and the unlink() technique of the “os” bundle to delete a selected file provided that it exists within the system. Each of those strategies basically have virtually related working. Each of those strategies take within the relative or the precise path of the file to be deleted, and if the file is discovered, they delete it. If the file shouldn’t be discovered, this system runs into an error. To keep away from this crash, merely use the try-except error dealing with statements.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments