"""
delete all .pyc bytecode files in a directory tree: use the
command line arg as root if given, else current working dir
"""

import os, sys
findonly = False
rootdir = os.getcwd() if len(sys.argv) == 1 else sys.argv[1]

found = removed = 0
for (thisDirLevel, subsHere, filesHere) in os.walk(rootdir):
    for filename in filesHere:
        if filename.endswith('.pyc'):
            fullname = os.path.join(thisDirLevel, filename)
            print('=>', fullname)
            if not findonly: 
                try: 
                    os.remove(fullname)
                    removed += 1
                except:
                    type, inst = sys.exc_info()[:2]
                    print('*'*4, 'Failed:', filename, type, inst)
            found += 1

print('Found', found, 'files, removed', removed)

[(11, 'os.walk', 'os.walk', False, 'import os, sys\n'), (8, 'os.getcwd', 'os.getcwd', False, 'import os, sys\n'), (14, 'os.path.join', 'os.path.join', False, 'import os, sys\n'), (18, 'os.remove', 'os.remove', False, 'import os, sys\n'), (21, 'sys.exc_info', 'sys.exc_info', False, 'import os, sys\n')]