Skip to main content

Python Remove .svn

This python script removes all .svn directories from a given path and can also remove the .DS_Store files mac os x so annoyingly puts in every directory.

text/x-python remove_svn_directories.py — 0 KB

File contents

DIRECTORY_TO_REMOVE_SVN_FROM = "/Applications/MAMP/htdocs/Resu"

REMOVE_MAC_DATASTORE_FILES = True

import os
from os.path import join

for root, dirs, files in os.walk(DIRECTORY_TO_REMOVE_SVN_FROM):
    if '.svn' in dirs:
        print 'removing .svn from %s' % root
        #must do it this way because rmdir does not allow deleting a non-empty directory
        for svn_root, svn_dirs, svn_files in os.walk(os.path.join(root, '.svn'), topdown=False):
            for name in svn_files:
                os.remove(os.path.join(svn_root, name))
            for name in svn_dirs:
                os.rmdir(os.path.join(svn_root, name))

        os.rmdir(os.path.join(root, '.svn'))
        
    if ".DS_Store" in files and REMOVE_MAC_DATASTORE_FILES:
        os.remove(os.path.join(root, '.DS_Store'))