Login

Delete python compiled files (*.pyc)

Author:
kogakure
Posted:
July 21, 2008
Language:
Python
Version:
.96
Tags:
files delete delpyc
Score:
-5 (after 9 ratings)

This simply deletes all compiled python files in a folder.

1
2
3
4
5
6
7
8
#!/usr/bin/env python

import os
directory = os.listdir('.')
for filename in directory:
    if filename[-3:] == 'pyc':
        print '- ' + filename
        os.remove(filename)

More like this

Comments

mk (on July 21, 2008):

And when would you use that instead of "rm *.pyc"? Or maybe "find -name '*.pyc' -print0|xargs -0 rm" to also remove .pyc files in subfolders?

#

jezdez (on July 21, 2008):

It's clearly a time saver.

#

dnordberg (on July 21, 2008):

or "find . -name ".pyc" -exec rm -rf {} \;"

#

bartTC (on July 21, 2008):

Or simpler:

find -name "*.pyc" -delete

#

lsbardel (on July 23, 2008):

It could be useful if it can delete all the .pyc in directory tree.

#

kogakure (on July 25, 2008):

Thanks all, the bash script seems much more useful.

#

trbs (on August 30, 2008):

personally i like:

find -type f -name "*.py[co]" -delete

#

douglasjarquin (on September 19, 2008):

Delete all .pyc files in a directory tree:

find . -name '*.pyc' -print0|xargs -0 rm

I love Python, but it is not always the best tool for the job.

#

vizualbod (on May 17, 2009):

On Solaris the following works: find . -name "*.pyc" -exec rm {} \;

#

vizualbod (on May 17, 2009):

hmm, comment system stripped out the backslash before semicolon

#

msmenzyk (on December 12, 2009):

This simple script removing all *.pyc files by python glob module. It works on all platforms.

file: pyc_cleaner.py

import glob

import os

glob.glob('\.pyc')

for filename in glob.glob('\.pyc'):

....os.remove(filename)

#

msmenzyk (on December 12, 2009):

Sorry... That script doesn't works because is not recursive... This is OK:

!/usr/bin/env python

import os import fnmatch

class GlobDirectoryWalker:

# a forward iterator that traverses a directory tree
def __init__(self, directory, pattern="*"):
    self.stack = [directory]
    self.pattern = pattern
    self.files = []
    self.index = 0

def __getitem__(self, index):
    while 1:
        try:
            file = self.files[self.index]
            self.index = self.index + 1
        except IndexError:
            # pop next directory from stack
            self.directory = self.stack.pop()
            self.files = os.listdir(self.directory)
            self.index = 0
        else:
            # got a filename
            fullname = os.path.join(self.directory, file)
            if os.path.isdir(fullname) and not os.path.islink(fullname):
                self.stack.append(fullname)
            if fnmatch.fnmatch(file, self.pattern):
                return fullname

for file in GlobDirectoryWalker(".", "*.pyc"): print file os.remove(file)

print "After..." for file in GlobDirectoryWalker(".", "*.pyc"): print file

#

dariusdamalakas (on June 14, 2010):

This is useful when you work on windows box.. :D Not every OS has that rich and user-friendly command line

#

dariusdamalakas (on June 14, 2010):

P.s. thanks for the command. I added it as django command rmpyc, does a job great!

A very handy tool to remove compiled classes, useful after doing lots of refactorings to remove old modules

#

mdrwxorg (on September 10, 2010):

shorter way to delete .pyc recursively:

[code]

!/usr/bin/env python

import os

def pycCleanup(directory,path): for filename in directory: if filename[-3:] == 'pyc': print '- ' + filename os.remove(path+os.sep+filename) elif os.path.isdir(path+os.sep+filename): pycCleanup(os.listdir(path+os.sep+filename),path+os.sep+filename)

directory = os.listdir('.') print('Deleting pyc files recursively in: '+ str(directory)) pycCleanup(directory,'.') [/code]

#

mdrwxorg (on September 10, 2010):

(once again, should be prettier now)

shorter way to delete .pyc recursively:

#!/usr/bin/env python

import os

def pycCleanup(directory,path):
    for filename in directory: 
        if     filename[-3:] == 'pyc': 
            print '- ' + filename 
            os.remove(path+os.sep+filename) 
        elif os.path.isdir(path+os.sep+filename): 
            pycCleanup(os.listdir(path+os.sep+filename),path+os.sep+filename)

directory = os.listdir('.')
print('Deleting pyc files recursively in: '+ str(directory)) 
pycCleanup(directory,'.')

#

kimiamania (on April 14, 2015):

on windows you could just run

del /S *.pyc

#

Please login first before commenting.