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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
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?
#
It's clearly a time saver.
#
or "find . -name ".pyc" -exec rm -rf {} \;"
#
Or simpler:
find -name "*.pyc" -delete
#
It could be useful if it can delete all the .pyc in directory tree.
#
Thanks all, the bash script seems much more useful.
#
personally i like:
find -type f -name "*.py[co]" -delete
#
Delete all .pyc files in a directory tree:
I love Python, but it is not always the best tool for the job.
#
On Solaris the following works: find . -name "*.pyc" -exec rm {} \;
#
hmm, comment system stripped out the backslash before semicolon
#
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)
#
Sorry... That script doesn't works because is not recursive... This is OK:
!/usr/bin/env python
import os import fnmatch
class GlobDirectoryWalker:
for file in GlobDirectoryWalker(".", "*.pyc"): print file os.remove(file)
print "After..." for file in GlobDirectoryWalker(".", "*.pyc"): print file
#
This is useful when you work on windows box.. :D Not every OS has that rich and user-friendly command line
#
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
#
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]
#
(once again, should be prettier now)
shorter way to delete .pyc recursively:
#
on windows you could just run
del /S *.pyc
#
Please login first before commenting.