Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 How to delete images in a folder

How do I delete all png format pics in a folder using Python 3?

like image 855
user2471181 Avatar asked Jun 28 '13 06:06

user2471181


1 Answers

This single line statement will take each file in a specified path and remove it if the filename ends in .png:

import os
os.remove(file) for file in os.listdir('path/to/directory') if file.endswith('.png')
like image 162
zeantsoi Avatar answered Sep 30 '22 05:09

zeantsoi