Python 3 Script to Remove All Files With Specific Extension in Directory Using OS Module

Python 3 Script to Remove All Files With Specific Extension in Directory Using OS Module

Python 3 Script to Remove All Files With Specific Extension in Directory Using OS Module

import os
directory = "./"

files_in_directory = os.listdir(directory)
print(files_in_directory)
filtered_files = [file for file in files_in_directory if file.endswith(".png")]

for file in filtered_files:
	path_to_file = os.path.join(directory, file)
	os.remove(path_to_file)
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment