This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Python - Remove files by extension

By: a guest on Aug 10th, 2013  |  syntax: Python  |  size: 0.49 KB  |  views: 78  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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)
clone this paste RAW Paste Data