Python Tutorials
Python File Handling
Python Modules
To delete a file, you must import the OS module, and use its os.remove () function:
Remove the file "demofile.txt":
import os
os.remove("demofile.txt")
To avoid error, you may want to check if the file exists before attempting to delete it:
Check if file exists, then delete it:
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
To delete an entire folder, use the os.rmdir () method:
Remove the folder "myfolder":
import os
os.rmdir("myfolder")
Note: You can only delete blank folders.