jump to content

Useless Python - delete flag files

Some scripts might be looking into some particular files in folders to do some thing. Here is a silly script to delete these files if some other conditions are met.

#
# vsbabu@hotmail.com  4/30/01
# pass it a valid directory as an argument
# recurse through that directory tree. if it sees html or asp files 
# in any directory  it will delete the flag files in that directory
# - I use it for forcing download from Zope.
#
import os, sys

debug = 0

#these are the files that will get deleted
del_files = ['.zmir','.zmir~']
#if the following extensions are there, then the above files will be deleted
check_exts = ['.html','.htm','.asp']

def clear_directory(in_dir):
    #get a list of files
    if debug==1: print in_dir
    try:
       l = os.listdir(in_dir)
    except:
       print 'Invalid directory. Exiting at ' + in_dir
       sys.exit(1)
    dirs = []
    found = 0
    for f in l:
        if debug==1: print f
        if (os.path.splitext(f)[1] in check_exts):
            found = 1
        if (os.path.isdir(in_dir + os.sep + f)):
            dirs.append(f)
    #see if any of the extensions are there
    if found == 1:
        print 'reset - ' + in_dir
        try:
            for r in del_files:
                os.remove(in_dir + os.sep + r)
        except:
            pass
    else:
        print 'ignore - ' + in_dir
    #walk the listing. recurse for any sub folders
    for d in dirs:
        if debug==1: print in_dir + os.sep + d
        clear_directory(in_dir + os.sep + d)
    return

# main
start_from = ""
try:
  start_from = sys.argv[1]
  clear_directory(start_from)
except:
  print 'Usage: ' + sys.argv[0] + ' full_path_of_starting_directory'
  print 'Please pass the full path of the starting directory as the only argument'
  sys.exit(1)