🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #115

Python Delete Files


Delete a File

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

Example

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

Check if File exist:

To avoid getting an error, you might want to check if the file exists before you try to delete it:

Example

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

Delete Folder

To delete an entire folder, use the os.rmdir() method:

Example

  import os
os.rmdir("myfolder")

Note: You can only remove empty folders.

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Python Delete Files – FAQs

Quick answers about learning Python Delete Files in Python.

This free note from CodingNow 2.0 explains Python Delete Files in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Python Delete Files, is 100% free with no signup required.
With focused practice, most students grasp Python Delete Files in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now