leftover pyc files

David Robinow drobinow at gmail.com
Thu Nov 3 20:14:31 EDT 2011


On Thu, Nov 3, 2011 at 1:54 PM, Andrea Crotti <andrea.crotti.0 at gmail.com> wrote:
> All these ideas (shell and git hooks) are nice, but unfortunately
> - it's svn not git
> - it's windows not *nix
> - we have to remove only the ones without the corresponding *py...
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Barely tested. Change the print functions to removes. Pass the top
directory as the argument.

import os, sys
for dirpath, dirnames, filenames in os.walk(sys.argv[1]):
    if dirpath.endswith("__pycache__"):
        thispath = dirpath[:-11]
        for f in filenames:
            if f.endswith(".pyc"):
                if not os.path.exists(os.path.join(thispath,f[:-1])):
                    print("delete " + os.path.join(thispath,f))

    else:
        for f in filenames:
            if f.endswith(".pyc"):
                if not os.path.exists(os.path.join(dirpath,f[:-1])):
                    print("delete " + os.path.join(dirpath,f))
                    #os.remove(os.path.join(dirpath,f))



More information about the Python-list mailing list