Created
November 12, 2009 14:13
-
-
Save lsbardel/232922 to your computer and use it in GitHub Desktop.
clean python binary files from a directory structure
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
import os | |
def rmgeneric(path, __func__): | |
try: | |
__func__(path) | |
#print 'Removed ', path | |
return 1 | |
except OSError, (errno, strerror): | |
print 'Could not remove %s, %s' % (path,strerror) | |
return 0 | |
def rmfiles(path, ext = None): | |
if not os.path.isdir(path): | |
return 0 | |
trem = 0 | |
tall = 0 | |
files = os.listdir(path) | |
for f in files: | |
fullpath = os.path.join(path, f) | |
if os.path.isfile(fullpath): | |
sf = f.split('.') | |
if len(sf) == 2: | |
if ext == None or sf[1] == ext: | |
tall += 1 | |
trem += rmgeneric(fullpath, os.remove) | |
elif os.path.isdir(fullpath): | |
r,ra = rmfiles(fullpath, ext) | |
trem += r | |
tall += ra | |
return trem, tall | |
if __name__ == '__main__': | |
path = os.curdir | |
removed, allfiles = rmfiles(path,'pyc') | |
print('removed %s pyc files out of %s' % (removed, allfiles)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment