#!/usr/bin/env python # -*- coding: iso-8859-1 -*- # (c) 2004 Thomas Güttler # This is open source software import os import sys import getopt del_endings=["~"] def usage(): print '''Usage: %s [-d STRING] dirs|files .... Delete backup files recursively. -d (optional): If a file ends with this string, it gets deleted You can give several -d options. Example: -d .pyc -d .pyo By default the list of endings is: %s ''' % (os.path.basename(sys.argv[0]), ', '.join(del_endings)) def delete_backup_file(dir): if not os.path.isdir(dir): return for file in os.listdir(dir): file=os.path.join(dir, file) if os.path.isdir(file): delete_backup_file(file) else: for ending in del_endings: if file.endswith(ending): print "Deleting: '%s' " % file try: os.unlink(file) except OSError: print "Fehler beim Löschen von %s" % ( file) def main(): try: opts, args = getopt.getopt(sys.argv[1:], "d:") except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) for o, a in opts: if o=="-d": del_endings.append(a) if len(args)==0: startdirs=["."] else: startdirs=args for file in startdirs: delete_backup_file(file) if __name__=="__main__": main()