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?
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?
find . -name "*.pyc" -exec rm -f {} \;
find . -name \*.pyc -delete
–
Collision {}
. What would happen if accidentally word-split and deleted an intermediate path which happens to be called like a fragment of the path you found? –
Dinitrobenzene alias rmPyCrap="find . -name \*.pyc -delete && find . -type d -name __pycache__ -delete"
–
Twentytwo -delete
is not part of the POSIX specification, and not guaranteed to exist in all implementations of find
. –
Prairial find . -name '*.pyc' -type f -delete
Surely the simplest.
find
supports -delete
; it is not part of the POSIX standard. –
Prairial __pycache__
directories. See my answer. –
Sepulture rm
not sufficient (drop -f
)? –
Partan -name
and delete your entire env! –
Holtorf -name '*.py?'
to include .pyo, .pyc,...
–
Ominous 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
find . -type d -name "__pycache__" -delete
will often give a warning about a missing path because apparently the contents will be added to the queue before the folder is deleted. 2>/dev/null
should fix that. –
Subscapular In current version of debian you have pyclean
script which is in python-minimal
package.
Usage is simple:
pyclean .
pyclean
(and now py3clean
) originate in a Debian package, and thus aren’t in RHEL. –
Partan pyclean
command is now available as a Python package installable from PyPI, which work cross-platform (including RHEL, macOS and Windows). It deletes any .pyc
file w/o special preference as of today. –
Mehalek 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
rm **/*.pyc
vs rm */*.pyc
? (The latter seemed to work for me locally.) –
Eisenstark rm */*.pyc
will delete all .pyc
files in all subdirectories of depth 1. rm **/*.pyc
will delete all .pyc
files in all subdirectories of depth 0+ –
Halberd **
acts as *
when run instead and running shopt globstar
throws an error). –
Eisenstark For windows users:
del /S *.pyc
I used to use an alias for that:
$ which pycclean
pycclean is aliased to `find . -name "*.pyc" | xargs -I {} rm -v "{}"'
find -print0
and xargs -0
instead. –
Hertz 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.
$ find . -name '*.pyc' -delete
This is faster than
$ find . -name "*.pyc" -exec rm -rf {} \;
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
find
, grep
and friends don't work there. –
Mehalek 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.
Just to throw another variant into the mix, you can also use backquotes like this:
rm `find . -name *.pyc`
rm
. –
Prairial full recursive
ll **/**/*.pyc
rm **/**/*.pyc
**
is redundant, and as in d0k
's answer, you must have globstar enabled in bash 4+ for this to work as intended. i.e. do shopt -s globstar
or have that in one of your sourced bash . files. –
Oberg ll
is commonly aliased to something like ls -l
, but isn't really a command or portable. So, to recursively list all .pyc
files in .
, you should instead do something like echo **/*.pyc
with globstar enabled –
Oberg There is the pyclean package on PyPI and Anaconda, which is easy to use and cross-platform.
python -m pip install pyclean
You then just need to run a simple command to clean all __pycache__
folders and *.pyc
files in current dir:
pyclean .
It also has additional options to delete various artifacts from running builds and tests.
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
First run:
find . -type f -name "*.py[c|o]" -exec rm -f {} +
Then add:
export PYTHONDONTWRITEBYTECODE=1
To ~/.profile
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.
find
with its -exec
predicate: find . -name '*.pyc' -exec rm {} +
(nicer, shorter, safer, robust, more efficient). –
Walls 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
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.
To delete all the python compiled files in current directory.
find . -name "__pycache__"|xargs rm -rf
find . -name "*.pyc"|xargs rm -rf
If you want remove all *.pyc
files and __pycache__
directories recursively in the current directory:
import os
os.popen('find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf')
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
py3clean
works for me!
cd /usr/local/lib/python3.9
sudo py3clean -v .
Had to add a few ignore params on M1:
pyclean --verbose . --ignore "Library",".Trash"
DEL /F /S /Q "Z:\aaa\*.pyc"
use cmd, or save in bat file, in window
In windows where find wont work. Here is a python function which can be run in ipython or python default interpretor or run as a script. Just putting for easy accessibility for some one.
from pathlib import Path
from os import remove
def remove_files(pth, filetype = "*.pyc"):
pth = Path(pth)
for file in pth.glob(f"**/{filetype}"):
remove(file)
You can call the function as:
target_folder = <path to clean>
remove_files(target_folder)
© 2022 - 2024 — McMap. All rights reserved.
rm
will be called with arguments [-r, a.pyc, b.pyc]. – Thumbsdown**/*.pyc
then you won't have to worry about it again – Hedge