Skip to content

Instantly share code, notes, and snippets.

@davechristian
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davechristian/9702997 to your computer and use it in GitHub Desktop.
Save davechristian/9702997 to your computer and use it in GitHub Desktop.
A little script that deletes all unwanted files from a specified path. Note that in it's current form it recursively enters all sub-directories. Example usage: 'python pirahna.py -p /home/bob/Music -x .db,.ini'
#! /usr/bin/env python
# Source is released under the MIT License
# See 'http://opensource.org/licenses/MIT' for more info
# Please be careful with this script! I take no responsibility
# for the accidental deletion of files resulting from use of it :)
import os
import sys
import getopt
def chomp_files(path, ext_list):
file_list = os.listdir(path)
for filename in file_list:
current_ext = os.path.splitext(filename)[1]
if os.path.isdir(os.path.join(path, filename)):
print("Entering " + filename)
chomp_files(os.path.join(path, filename), ext_list)
else:
if (current_ext in ext_list):
os.remove(os.path.join(path, filename))
print("Deleted " + filename)
if __name__ == "__main__":
usage = 'Usage: pirhana.py -p <path> -x <.ext1,.ext2,.etc>'
ext_list = []
path = ''
try:
# grab the command line options and arguments
opts, args = getopt.getopt(sys.argv[1:],"p:x:")
except getopt.GetoptError:
print(usage)
sys.exit(2)
for o, a in opts:
if o == "-x":
ext_list = a.split(',') # grab the list of extensions
elif o == "-p":
path = a # grab the starting path
# no params? Print error and bomb out
if len(ext_list) == 0 or len(path) == 0:
print(usage)
sys.exit()
chomp_files(path, ext_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment