Delete any file containing a given string





4
Date Submitted Fri. Aug. 18th, 2006 7:25 AM
Revision 1 of 1
Helper axsaxs
Tags Delete | Files | pattern
Comments 0 comments
Walks every subdirectory starting from [initial_dir] and deletes every file in it that contains in its filename a [string]

"""
         FilesDeleter.py

         Usage: python FilesDeleter.py [initial_dir] [string]
        
         Walks every subdirectory starting from
         [initial_dir] and deletes every file in it that
         contains in its file name 'string'
         
         Example: python FilesDeleter.py . .class
         Deletes every .class file in all subdirs starting from current.
        
"
""

import os, sys

def deletefiles(dst, extension):
    for root, dirs, files in os.walk(dst):
        for name in files:
            # If name ends with 'extension' kill it
            if (name.rfind(extension)>0):
                print 'deleting', os.path.join(root, name)
                os.remove(os.path.join(root, name))

if __name__ == "__main__":
    print "FilesDeleter v.0.2"
    if (len(sys.argv)>1):
        deletefiles(sys.argv[1],sys.argv[2])
    else:
        print """
         FilesDeleter.py

         Usage: python FilesDeleter.py [initial_dir] [string]
        
         Walks every subdirectory starting from
         [initial_dir] and deletes every file in it that
         contains in its file name 'string'
         
         Example: python FilesDeleter.py . .class
         Deletes every .class file in all subdirs starting from current.
        
"
""
 

Alessio Saltarin

axsaxs.altervista.org

Comments

There are currently no comments for this snippet.

Voting

Votes Down