また使いそうなので記録。指定ディレクトリ内の.svnやらCVSやらを一括削除するためのスクリプト。
バグがあるかもしれないので、削除処理をコメントにして削除対象を確認できるようにしておく。
recursive_delete.py
# -*- coding: utf-8 -*- import os import sys import re if len(sys.argv) < 3: print "Usage: python recursive_delete.py ROOT_DIR TARGET" exit() root_dir = sys.argv[1] target_name = sys.argv[2] if not os.path.exists(root_dir): print "directory (%s) does not exist." % root_dir exit() file_path_list = [] dir_path_list = [] pattern = re.compile(target_name, re.I) for root, dirs, files in os.walk(root_dir): for f in files: target_path = os.path.join(root, f) if pattern.search(target_path): file_path_list.append(target_path) for d in dirs: target_path = os.path.join(root, d) if pattern.search(target_path): dir_path_list.append(target_path) for target_path in file_path_list: print "removing file %s" % target_path #os.remove(target_path) dir_path_list.reverse() for target_path in dir_path_list: print "removing dir %s" % target_path #os.rmdir(target_path)