How you will delete a file in python? In this tutorial, you will learn about Python delete files (single or multiples) if the file exists. Must recommend reading the Python Create File because in this tutorial we are deleting the same files, which create in previous tutorials.

How to Python Delete File?
To delete a file in python, you must import the OS module, and run itsos.remove()
function or other modules with functions. You must check before the file is available or not, else the program will throw an error.
Delete methods in Python
This is the python delete methods for file and folders.
os.remove()
– Remove (delete) the file path.os.rmdir()
will remove an empty directory.shutil.rmtree()
will delete a directory and all its contents.
Python syntax to delete a file
You must import the OS module to delete a file in python.
import os os.remove("/path/<file_name>.txt")
OR
if the file in the same place of project
import os os.remove("fileName.txt")
Python Delete File Example
First, check whether the file or folder exists or not then only delete that file. This can be achieved in two ways :
- os.path.isfile(“/path/fileName”)
- or use exception handling.
We assume have a file in project “cFile.txt.” , for detail read the previous tutorial Create File tutorial. Check below the example program of how to python delete a file if exists.
import os if os.path.exists("cFile.txt"): os.remove("cFile.txt") else: print('File does not exists')
if the file does not exist then the output will be print() “File does not exist”
Python Delete Folder/Directory
To delete an entire folder, you have to use the os.rmdir()
method. It will delete the only empty folder. For complete delete for the file in a folder using shutil.rmtree().
import os os.rmdir("folderName")
Delete multiple files
To delete multiple files, just loop over your list of files and use the above os.rmdir()
function.
To delete a folder containing all files you want to remove have to import shutil
package. Then you can remove the folder as follows.
import shutil shutil.rmtree('my_folder')
Do comment if you have in doubt or suggestion or code. This chapter comes under the Python File Handling section.
Exercise | Practice
- How do I delete a specified number of files in a directory in Python?
- How to remove a file if exists and handle errors?
Python File Handling Quiz
Quiz-summary
0 of 10 questions completed
Questions:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Information
Python File Handling to test your knowledge – ( Create, Open, Update, delete and more about in Python)
It’s for Beginners, Advanced and Experienced Programmers.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Results
0 of 10 questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 points, (0)
Average score |
|
Your score |
|
Categories
- Not categorized 0%
Pos. | Name | Entered on | Points | Result |
---|---|---|---|---|
Table is loading | ||||
No data available | ||||
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- Answered
- Review
-
Question 1 of 10
1. Question
Which of the following command is used to open a file “c:\textFile.txt” in read-mode only?
Correct
Incorrect
-
Question 2 of 10
2. Question
Which functions use to check if a file exists?
File name = “logo”Correct
Incorrect
-
Question 3 of 10
3. Question
How do you insert something on a new line in a file?
Correct
Incorrect
-
Question 4 of 10
4. Question
Appending to a file means adding extra data into the file.
Correct
Incorrect
-
Question 5 of 10
5. Question
What is the last action that must be performed on a file?
Correct
Incorrect
-
Question 6 of 10
6. Question
What is the data type of data read from a file?
Correct
Incorrect
-
Question 7 of 10
7. Question
Can you create a file without using file modes in Python?
Correct
Incorrect
-
Question 8 of 10
8. Question
Reading from a file often involves using…
Correct
Incorrect
-
Question 9 of 10
9. Question
Which of the following statements are true regarding the opening modes of a file?
Correct
Incorrect
-
Question 10 of 10
10. Question
Which of the following commands option right to read the entire contents of a file as a string using the file object
? Correct
Incorrect
Do comment if you have any doubt and suggestion on this tutorial.
Note
: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
Python 3.7
All Example Python deletefile if exists are in Python 3, so it may change its different from python 2 or upgraded versions.

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.