Python Tutorials · File Handling

Python Delete Files

Learn all about Python Delete Files in this comprehensive tutorial.

5 min read intermediate
  • To delete a file, you must import the OS module, and run its os.
  • To avoid getting an error, you might want to check if the file exists before you try to delete it:
  • To delete an entire folder, use the os.

Delete a File

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

python

Check if File exist:

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

python

Delete Folder

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

python
Note: Note: You can only remove empty folders.

Module quiz

2 questions
1

Which of the following is true about Python Delete Files?

2

What is the most common pitfall when working with Python Delete Files?

Answer all questions to submit.