Last active
December 16, 2015 16:59
-
-
Save cbsmith/5467033 to your computer and use it in GitHub Desktop.
Python code that traverses one or more directories, removing any orphaned .pyc's.
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
"""Traverses the directory tree deleting any .pyc's who do not have a source .py. | |
Helpful when switching between revisions with source control.""" | |
from os.path import join | |
from os import walk, unlink | |
import re | |
import sys | |
PYTHON_RE = re.compile(R'^(.*\.py)(c)?$') | |
global count | |
count = 0 | |
directories = sys.argv[1:] if len(sys.argv) > 1 else ['.'] | |
for directory in directories: | |
for (dirpath, dirnames, filenames) in walk(directory): | |
source_set = set() | |
pyc_set = set() | |
for filename in filenames: | |
match = PYTHON_RE.match(filename) | |
if match is not None: | |
(source_set if match.group(2) is None else pyc_set).add(match.group(1)) | |
to_remove = (pyc_set - source_set) | |
for extra in to_remove: | |
unlink(join(dirpath, extra + 'c')) | |
count += 1 | |
if count > 0: | |
print 'Removed \033[91m%d\033[0m file%s' % (count, 's' if count > 1 else '') |
The code as is traverses whatever directories you pass as arguments. It might make sense to filter against paths that are in the PYTHONPATH, but I'd not want to traverse the entire PYTHONPATH as usually that isn't all version controlled.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if you have lots of unrelated directories. Couldn't this be improved by only walking PYTHONPATH?