Python File Handling

Python Delete Files

Deleting files

Python Delete Files

To delete a file, you must import the OS module, and run its os.remove() function.

Delete a File

import os
os.remove("demofile.txt")

Check if File Exists

import os
if os.path.exists("demofile.txt"):
    os.remove("demofile.txt")
else:
    print("The file does not exist")

Delete Folder

import os
os.rmdir("myfolder")