Created
May 23, 2014 02:06
-
-
Save muxuezi/fc9b2a4a8ed16dc8029e to your computer and use it in GitHub Desktop.
clean pyc files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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