ItSolutionStuff.com

Python Delete Files with Specific Extension Example

By Hardik Savani • October 30, 2023
Python

Hi,

This post will give you example of python delete files with specific extension. This article will give you simple example of remove files with specific extension python. We will use python delete all images with specific extension. I would like to show you delete all files with extension python. you will do the following things for python remove file by extension.

Sometimes, we need to delete a specific file extension. in this example we will remove only ".png" files from files folder. so let's see below example code.

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

let's see below simple example with output:

Example:

main.py

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")

Output:

All .png files removed successfully

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Python Delete File if Exists Example

Read Now →

Python Post Request with pem File Example

Read Now →

Python Post Request with Json File Example

Read Now →

Python Post Request with Basic Authentication Example

Read Now →

Python Post Request with Bearer Token Example

Read Now →

Python Copy File From one Directory to Another Example

Read Now →

Python Get File Extension from Filename Example

Read Now →

Python Create Zip Archive from Directory Example

Read Now →

How to Check if Today is Friday or not in Python?

Read Now →

Python PATCH Request with Parameters Example

Read Now →

How to Get Current Time in Python?

Read Now →