If your source changes; CPython will recompile and re-cached the bytecode.
to avoid that I came up with an shell command or alternatively a python script to get rid of pyc or files ending with '~' tilde .
Find files ending with pyc or ~
find . -type f \( -name "*~" -o -name "*pyc" \)
below code will find and delete the files as well.
find . -type f \( -name "*~" -delete -o -name "*pyc" -delete \)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#!/usr/bin/env python import os import sys def removeAllPycFiles(base_dir): """ Scan directories for .pyc files with a .py file and delete them. """ for dirname, _, filenames in os.walk(base_dir): for filename in filenames: root, ext = os.path.splitext(filename) if ext not in ['.pyc', '.py~']: continue pyc_path = os.path.join(dirname, filename) py_path = os.path.join(dirname, root + '.py') try: # don't delete pyc that don't have py source if os.path.exists(py_path): os.remove(pyc_path) print "Deleted %s" % pyc_path except OSError, er: print er # Wrap OS calls in try/except in case another process touched this file. pass if __name__ == '__main__': for path in sys.argv[1:]: print "Deleting all pyc files in %s " % os.path.abspath(path) removeAllPycFiles(path) |