bash 如何从项目中删除所有 .pyc 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/785519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:07:43  来源:igfitidea点击:

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

bash

提问by Teifion

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:

我在一个相当大的项目中重命名了一些文件,并想删除它们留下的 .pyc 文件。我尝试了 bash 脚本:

 rm -r *.pyc

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

但这并没有像我想象的那样在文件夹中递归。我究竟做错了什么?

回答by Bill the Lizard

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

回答by Andy Baker

find . -name '*.pyc' -delete

find . -name '*.pyc' -delete

Surely the simplest.

当然是最简单的了。

回答by jb.

In current version of debian you have pycleanscript which is in python-minimalpackage.

在当前版本的 debian 中,您有包中的pyclean脚本python-minimal

Usage is simple:

用法很简单:

pyclean .

回答by Wilfred Hughes

Add to your ~/.bashrc:

添加到您的~/.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.

这将删除所有 .pyc 和 .pyo 文件和__pycache__目录。它也非常快。

Usage is simply:

用法很简单:

$ cd /path/to/directory
$ pyclean

回答by d0k

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

如果您使用 bash >=4.0(或 zsh)

rm **/*.pyc

Note that */*.pycselects all .pycfiles in the immediate first-level subdirectories while **/*.pycrecursively scans the whole directory tree. As an example, foo/bar/qux.pycwill be deleted by rm **/*.pycbut not by */*.pyc.

请注意,*/*.pyc选择.pyc直接一级子目录中的所有文件,同时**/*.pyc递归扫描整个目录树。例如,foo/bar/qux.pyc将被删除,rm **/*.pyc但不会被删除*/*.pyc

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

必须启用 globstar shell 选项。要启用globstar

shopt -s globstar

and to check its status:

并检查其状态:

shopt globstar

回答by miku

I used to use an alias for that:

我曾经为此使用别名:

$ which pycclean

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

回答by haki

For windows users:

对于 Windows 用户:

del /S *.pyc

回答by Ron Romero

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.

find 递归地查找 *.pyc 文件。xargs 获取该名称列表并将其发送到 rm。-print0 和 -0 告诉这两个命令用空字符分隔文件名。这允许它在包含空格的文件名,甚至包含新行的文件名上正确工作。

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.

使用 -exec 的解决方案有效,但它会为每个文件启动一个新的 rm 副本。在缓慢的系统或有大量文件的情况下,这将花费太长时间。

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.

iname 添加了不区分大小写的功能,例如 *.PYC 。如果您没有此类文件,则 no-run-if-empty 可防止您从 rm 收到错误消息。

回答by Moxdata

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

This is faster than

这比

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

回答by Mithril

Further, people usually want to remove all *.pyc, *.pyofiles and __pycache__directories recursively in the current directory.

此外,人们通常希望递归删除当前目录中的所有*.pyc,*.pyo文件和__pycache__目录。

Command:

命令:

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