لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science File Handling In Python: Deleting A File

How to delete a file in Python?

Delete a File: In order to delete a file, you must import the OS module, and run its os.remove() function.

Example 12: Remove the file "demofile.txt

Code

import os

if os.path.exists("mynewexample.txt"):

os.remove("mynewexample.txt")

else:

print("The file does not exist")

How to delete a folder?

To delete an entire folder, we have to use the os.rmdir() method.

Example 13: Remove the folder "myfolder".

Code

import os

os.rmdir("myfolder")

Note You can only delete an empty foder only.