How to Delete Files with Specific Extension in Python?

Published On: 28/10/2022 | Category: Python

Hi Guys,

Today our leading topic is how to delete files with specific extension in python. you can see python delete files with specific extension. I explained simply step by step python delete all images with specific extension. I’m going to show you about delete all files with extension python. follow bellow step for python remove file by extension.

We may need to remove a certain file extension. In this example, we'll solely purge the files folder of ".png" files. Let's look at some example code below.

You can use these examples with python3 (Python 3) version.

So let's see bellow example:

import os
from os import listdir
folderPath = 'files/';
# Get All List of Files
for fileName in listdir(folderPath):
    #Check file extension
    if fileName.endswith('.png'):
        # Remove File
        os.remove(folderPath + fileName)
print("All .png files removed successfully")
All .png files removed successfully

It will help you....

Happy Pythonic Coding!