"""
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, 43, 11, 59), 'os.walk', 'os.walk', ({(11, 51, 11, 58): 'rootdir'}, {}), '(rootdir)', False, 'import os, sys\n'), ((8, 10, 8, 21), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os, sys\n'), ((14, 23, 14, 59), 'os.path.join', 'os.path.join', ({(14, 36, 14, 48): 'thisDirLevel', (14, 50, 14, 58): 'filename'}, {}), '(thisDirLevel, filename)', False, 'import os, sys\n'), ((18, 20, 18, 39), 'os.remove', 'os.remove', ({(18, 30, 18, 38): 'fullname'}, {}), '(fullname)', False, 'import os, sys\n'), ((21, 33, 21, 47), 'sys.exc_info', 'sys.exc_info', ({}, {}), '()', False, 'import os, sys\n')]