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

Bash

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

find . -name "*.pyc" -exec rm -f {} \;

Solution 2 - Bash

find . -name '*.pyc' -delete

Surely the simplest.

Solution 3 - Bash

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

Solution 4 - Bash

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

Usage is simple:

pyclean .

Solution 5 - Bash

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

Solution 6 - Bash

For windows users:

del /S *.pyc

Solution 7 - Bash

I used to use an alias for that:

$ which pycclean

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

Solution 8 - Bash

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

The find recursively looks for *.pyc files. The xargs takes that list of names and sends it to rm. The -print0 and the -0 tell the two commands to seperate the filenames with null characters. This allows it to work correctly on file names containing spaces, and even a file name containing a new line.

The solution with -exec works, but it spins up a new copy of rm for every file. On a slow system or with a great many files, that'll take too long.

You could also add a couple more args:

find . -iname '*.pyc' -print0 | xargs -0 --no-run-if-empty  rm

iname adds case insensitivity, like *.PYC . The no-run-if-empty keeps you from getting an error from rm if you have no such files.

Solution 9 - Bash

$ find . -name '*.pyc' -delete

This is faster than

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

Solution 10 - Bash

Further, people usually want to remove all *.pyc, *.pyo files and __pycache__ directories recursively in the current directory.

Command:

find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf

Solution 11 - Bash

Django Extension

> Note: This answer is very specific to Django project that have already been using Django Extension.

python manage.py clean_pyc

The implementation can be viewed in its source code.

Solution 12 - Bash

Just to throw another variant into the mix, you can also use backquotes like this:

rm `find . -name *.pyc`

Solution 13 - Bash

full recursive

ll **/**/*.pyc
rm **/**/*.pyc

Solution 14 - Bash

if you don't want .pyc anymore you can use this single line in a terminal:

export PYTHONDONTWRITEBYTECODE=1

if you change your mind:

unset PYTHONDONTWRITEBYTECODE

Solution 15 - Bash

Now there is a package pyclean on PyPI, which is easy to use, and cross-platform. User just need a simple command line to clean all __pycache__ files in current dir:

pyclean .

Solution 16 - Bash

First run:

find . -type f -name "*.py[c|o]" -exec rm -f {} +

Then add:

export PYTHONDONTWRITEBYTECODE=1

To ~/.profile

Solution 17 - Bash

rm -r recurses into directories, but only the directories you give to rm. It will also delete those directories. One solution is:

for i in $( find . -name *.pyc )
do
  rm $i
done

find will find all *.pyc files recursively in the current directory, and the for loop will iterate through the list of files found, removing each one.

Solution 18 - Bash

find . -name "*.pyc"|xargs rm -rf

Solution 19 - Bash

You can run find . -name "*.pyc" -type f -delete.

But use it with precaution. Run first find . -name "*.pyc" -type f to see exactly which files you will remove.

In addition, make sure that -delete is the last argument in your command. If you put it before the -name *.pyc argument, it will delete everything.

Solution 20 - Bash

If you want to delete all the .pyc files from the project folder.

First, you have

cd <path/to/the/folder>

then find all the .pyc file and delete.

find . -name \*.pyc -delete

Solution 21 - Bash

To delete all the python compiled files in current directory.

find . -name "__pycache__"|xargs rm -rf
find . -name "*.pyc"|xargs rm -rf

Solution 22 - Bash

If you want remove all *.pyc files and __pycache__ directories recursively in the current directory:

  • with python:
import os

os.popen('find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf')
  • or manually with terminal or cmd:
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTeifionView Question on Stackoverflow
Solution 1 - BashBill the LizardView Answer on Stackoverflow
Solution 2 - BashAndy BakerView Answer on Stackoverflow
Solution 3 - BashWilfred HughesView Answer on Stackoverflow
Solution 4 - Bashjb.View Answer on Stackoverflow
Solution 5 - Bashd0kView Answer on Stackoverflow
Solution 6 - BashhakiView Answer on Stackoverflow
Solution 7 - BashmikuView Answer on Stackoverflow
Solution 8 - BashRon RomeroView Answer on Stackoverflow
Solution 9 - BashMoxdataView Answer on Stackoverflow
Solution 10 - BashMithrilView Answer on Stackoverflow
Solution 11 - BashYeoView Answer on Stackoverflow
Solution 12 - BashClint MillerView Answer on Stackoverflow
Solution 13 - BashromuloigorView Answer on Stackoverflow
Solution 14 - BashKevin SabbeView Answer on Stackoverflow
Solution 15 - Bashmo-hanView Answer on Stackoverflow
Solution 16 - BashMichael BeninView Answer on Stackoverflow
Solution 17 - BashChris LutzView Answer on Stackoverflow
Solution 18 - BashPiyusGView Answer on Stackoverflow
Solution 19 - BashgpapView Answer on Stackoverflow
Solution 20 - BashGirish VasView Answer on Stackoverflow
Solution 21 - BashAB AbhiView Answer on Stackoverflow
Solution 22 - BashMilovan TomaševićView Answer on Stackoverflow