Skip to content

Instantly share code, notes, and snippets.

@tritium21
Created April 1, 2015 02:42
Show Gist options
  • Save tritium21/dd616d939678d73296a8 to your computer and use it in GitHub Desktop.
Save tritium21/dd616d939678d73296a8 to your computer and use it in GitHub Desktop.
import argparse
import logging
import os
import sys
def kill_it(
base_path=os.getcwd(),
thumbsdb='Thumbs.db',
unlink=True,
):
for dirpath, dirnames, filenames in os.walk(base_path):
if thumbsdb not in filenames:
continue
killable = os.path.join(dirpath, thumbsdb)
logging.info("rm: %s", killable)
if unlink:
os.unlink(killable)
def main(argv=None):
CWD = os.getcwd()
parser = argparse.ArgumentParser(
description="Kill evil files, recursively"
)
parser.add_argument(
'base_path',
nargs='?',
default=CWD,
metavar="PATH",
help=(
"Root directory in which to search for evil files."
" Default: '%s'" % (CWD,)
),
)
parser.add_argument(
'-f',
'--file',
dest='thumbsdb',
default='Thumbs.db',
metavar='FILE',
help="File name to search for. Default: 'Thumbs.db'"
)
me = parser.add_mutually_exclusive_group()
me.add_argument(
'-n',
'--noop',
action='store_false',
dest='unlink',
help="Do nothing, just search."
)
me.add_argument(
'-q',
'--quiet',
action='store_const',
dest='loglevel',
const=logging.ERROR,
default=logging.INFO,
help="Be quiet."
)
if argv is None:
args = parser.parse_args()
else:
args = parser.parse_args(argv)
logging.basicConfig(
format='%(message)s',
level=args.loglevel
)
del args.loglevel
kill_it(**vars(args))
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment