Deleting files in Python allows programs to remove unwanted or temporary files from the system.

Delete a File :
              The most common way to delete a file is using the os module.

Syntax :
Python
import os os.remove("filename")

Example :
Python
import os os.remove("data.txt") 

Delete a File using os.unlink( ) :
                    It is similar to os.remove( ) .
Python
import os os.unlink("data.txt") 

Using try - except :
Python
import os try: os.remove("data.txt") except FileNotFoundError: print("File not found") 

Deleting a Folder :

Python
import os os.rmdir("myfolder") # removes empty folder 

Python
import shutil shutil.rmtree("myfolder") # deletes a folder and all its contents