Skip to content

Instantly share code, notes, and snippets.

@muxuezi
Created May 23, 2014 02:06
Show Gist options
  • Save muxuezi/fc9b2a4a8ed16dc8029e to your computer and use it in GitHub Desktop.
Save muxuezi/fc9b2a4a8ed16dc8029e to your computer and use it in GitHub Desktop.
clean pyc files
"""
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
rootdir = os.getcwd() if len(sys.argv) < 2 else sys.argv[1]
findonly = False if len(sys.argv) < 3 else int(sys.argv[2])
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment