Advertisement
Advertisement


How do I remove all .pyc files from a project?


Question

I've renamed some files in a fairly large project and want to remove the .pyc files they've left behind. I tried the bash script:

 rm -r *.pyc

But that doesn't recurse through the folders as I thought it would. What am I doing wrong?

2016/04/02
1
685
4/2/2016 2:28:03 AM

Accepted Answer

find . -name "*.pyc" -exec rm -f {} \;
2018/07/17
1191
7/17/2018 9:27:03 AM


In current version of debian you have pyclean script which is in python-minimal package.

Usage is simple:

pyclean .
2012/12/02

Add to your ~/.bashrc:

pyclean () {
        find . -type f -name "*.py[co]" -delete
        find . -type d -name "__pycache__" -delete
}

This removes all .pyc and .pyo files, and __pycache__ directories. It's also very fast.

Usage is simply:

$ cd /path/to/directory
$ pyclean
2014/04/07

If you're using bash >=4.0 (or zsh)

rm **/*.pyc

Note that */*.pyc selects all .pyc files in the immediate first-level subdirectories while **/*.pyc recursively scans the whole directory tree. As an example, foo/bar/qux.pyc will be deleted by rm **/*.pyc but not by */*.pyc.

The globstar shell options must be enabled. To enable globstar:

shopt -s globstar

and to check its status:

shopt globstar
2017/01/30

I used to use an alias for that:

$ which pycclean

pycclean is aliased to `find . -name "*.pyc" | xargs -I {} rm -v "{}"'
2013/08/16

For windows users:

del /S *.pyc
2015/10/23

Source: https://stackoverflow.com/questions/785519
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]