Created
July 8, 2011 09:32
-
-
Save oleiade/1071449 to your computer and use it in GitHub Desktop.
Recursive clean script, removes every given -f option filetype from current base directory and in subdirectories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import fnmatch, subprocess | |
import sys, os | |
import argparse | |
def generate_filename_pattern(pattern): | |
try: | |
if pattern[0] == '.': | |
return '*' + pattern | |
else: | |
return '*.' + pattern | |
except IndexError: | |
print "Error: incorrect pattern" | |
def gen_argument_parser(): | |
""" | |
Parse Arguments: Add a -f option in order to manage which | |
Filtype to match in order to cleanup selected directory | |
""" | |
parser = argparse.ArgumentParser(description="Process some file types") | |
parser.add_argument('-f', action="store", | |
dest='file_type', help="Defines which filetype to match", | |
default=False) | |
result = parser.parse_args() | |
return result | |
rootPath = '.' | |
emacs_patterns = ['*~', '#*#'] | |
patterns = [] | |
parser_result = gen_argument_parser() | |
if parser_result.file_type: | |
patterns.append(generate_filename_pattern(parser_result.file_type)) | |
else: | |
patterns = emacs_patterns | |
for root, dirs, files in os.walk(rootPath): | |
for pattern in patterns: | |
for filename in fnmatch.filter(files, pattern): | |
print os.path.join(root, filename) | |
subprocess.Popen(['rm', os.path.join(root, filename)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment