Tuesday, June 10, 2014

Script to remove jpg files recursively

I am using a webcam which uploads files via FTP. Unfortunately the cam which uploads the files does not clean up old files. It's actually a set of cameras which all write file to their own directory structures. So when I need a script to clean up the old jpg files it needs to run recursively:

 #!/usr/bin/env python  
 import re  
 import os  
 __dir__ = os.path.dirname(os.path.realpath(__file__))  
 def cleanup_recursive(_dir=__dir__):  
   print "Cleaning " + _dir  
   for _item in os.listdir(_dir):  
     _path = _dir + "/" + _item  
     if os.path.isfile(_path) and re.match(".+\.jpg$", _path):  
       os.remove(_path)  
     elif os.path.isdir(_path):  
       cleanup_recursive(_dir)  
 cleanup_recursive()  

No comments:

Post a Comment