Delete python compiled files (*.pyc)

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)

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

#

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.