Skip to content

Instantly share code, notes, and snippets.

@masnun
Created August 15, 2012 23:17
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 masnun/3364613 to your computer and use it in GitHub Desktop.
Save masnun/3364613 to your computer and use it in GitHub Desktop.
Remove .pyc files recursively (shell command + django manage.py plugin)
# File: <appname>/management/commands/rmpyc.py
# management and commands directories must be python packages (each must have a __init__.py file)
from django.core.management.base import BaseCommand, CommandError
from subprocess import Popen, STDOUT, PIPE
import shlex, os
class Command(BaseCommand):
def handle(self, *args, **options):
#Cleanup the *.pyc files using *nix shell
# Doesn't and shouldn't work on non-*nix systems
os.system("rm `find . -type f -name '*.pyc'`")
#Find pyc files to test outcomes of the command
cmd = "find . -type f -name '*.pyc'"
args = shlex.split(cmd)
files = Popen(args, stderr=STDOUT, stdout=PIPE).communicate()[0].split("\n")
if len(files) < 2:
self.stdout.write('All *pyc files were succesfully cleaned up! \n')
else:
self.stdout.write('Whoops! Something went wrong! \n')
rm `find . -type f -name "*.pyc"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment