SHARE
TWEET

Python - Remove files by extension

a guest Aug 10th, 2013 79 Never
  1. import os
  2.  
  3. def remove_by_ext(path,ext):
  4. '''Walks through directories removing files with specified extension'''
  5.     for (path,dirs,files) in os.walk(path):
  6.         for file_name in files:
  7.             file_name_lower=file_name.lower()
  8.             if file_name_lower[-len(ext):] == ext.lower():
  9.                 os.remove(path+'\\'+file_name)
  10.                 print('File removed: %s' %file_name)
  11.  
  12. path = raw_input('Input Path: ')
  13. ext = raw_input('Input Extension: ')
  14. remove_by_ext(path,ext)
RAW Paste Data
Top