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?
但它不会像我想的那样在文件夹中递归。我做错了什么?
17 个解决方案
#1
944
find . -name "*.pyc" -exec rm -f {} \;
as mentioned in the comments, you can also use the -delete
action
正如注释中提到的,您还可以使用-delete操作
find . -name \*.pyc -delete
#2
884
find . -name '*.pyc' -delete
找到。- name”*。佩克的删除
Surely the simplest.
当然最简单的。
#3
78
In current version of debian you have pyclean
script which is in python-minimal
package.
在debian的当前版本中,您有pyclean脚本,它在python-极小包中。
Usage is simple:
使用很简单:
pyclean .
#4
66
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
#5
46
If you're using bash >=4.0 (or zsh)
如果使用bash >=4.0(或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
.
注意* / *。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
#6
30
I used to use an alias for that:
我曾经用化名
$ which pycclean
pycclean is aliased to `find . -name "*.pyc" | xargs -I {} rm -v "{}"'
#7
16
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.
查找循环查找*。佩克文件。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:
你也可以添加一些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增加了大小写不敏感,如*。佩克。如果没有这样的文件,那么无运行假设为空将使您无法从rm获得错误。
#8
15
$ find . -name '*.pyc' -delete
This is faster than
这是速度比
$ find . -name "*.pyc" -exec rm -rf {} \;
#9
9
For windows users:
对于windows用户:
del /S *.pyc
#10
8
Just to throw another variant into the mix, you can also use backquotes like this:
为了将另一种变体加入进来,你还可以使用这样的引号:
rm `find . -name *.pyc`
#11
7
Django Extension
Note: This answer is very specific to Django project that have already been using Django Extension.
注意:这个答案非常特定于已经使用Django扩展的Django项目。
python manage.py clean_pyc
The implementation can be viewed in its source code.
实现可以在源代码中查看。
#12
6
full recursive
全递归
ll **/**/*.pyc
rm **/**/*.pyc
#13
5
Further, people usually want to remove all *.pyc
, *.pyo
files and __pycache__
directories recursively in the current directory.
此外,人们通常希望删除所有*。佩克,*。在当前目录中递归地使用pyo文件和__pycache__目录。
Command:
命令:
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
#14
3
First run:
第一次运行:
find . -type f -name "*.py[c|o]" -exec rm -f {} +
Then add:
然后添加:
export PYTHONDONTWRITEBYTECODE=1
To ~/.profile
~ / . profile
#15
3
find . -name "*.pyc"|xargs rm -rf
#16
2
rm -r
recurses into directories, but only the directories you give to rm
. It will also delete those directories. One solution is:
rm -r递归到目录中,但只返回给rm的目录。它还将删除这些目录。一个解决方案是:
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.
发现将发现所有*。在当前目录中递归地使用pyc文件,for循环将遍历找到的文件列表,删除每个文件。
#17
0
if you don't want .pyc anymore you can use this single line in a terminal:
如果你不想要。pyc你可以在终端中使用这条线:
export PYTHONDONTWRITEBYTECODE=1
if you change your mind:
如果你改变主意:
unset PYTHONDONTWRITEBYTECODE
#1
944
find . -name "*.pyc" -exec rm -f {} \;
as mentioned in the comments, you can also use the -delete
action
正如注释中提到的,您还可以使用-delete操作
find . -name \*.pyc -delete
#2
884
find . -name '*.pyc' -delete
找到。- name”*。佩克的删除
Surely the simplest.
当然最简单的。
#3
78
In current version of debian you have pyclean
script which is in python-minimal
package.
在debian的当前版本中,您有pyclean脚本,它在python-极小包中。
Usage is simple:
使用很简单:
pyclean .
#4
66
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
#5
46
If you're using bash >=4.0 (or zsh)
如果使用bash >=4.0(或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
.
注意* / *。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
#6
30
I used to use an alias for that:
我曾经用化名
$ which pycclean
pycclean is aliased to `find . -name "*.pyc" | xargs -I {} rm -v "{}"'
#7
16
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.
查找循环查找*。佩克文件。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:
你也可以添加一些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增加了大小写不敏感,如*。佩克。如果没有这样的文件,那么无运行假设为空将使您无法从rm获得错误。
#8
15
$ find . -name '*.pyc' -delete
This is faster than
这是速度比
$ find . -name "*.pyc" -exec rm -rf {} \;
#9
9
For windows users:
对于windows用户:
del /S *.pyc
#10
8
Just to throw another variant into the mix, you can also use backquotes like this:
为了将另一种变体加入进来,你还可以使用这样的引号:
rm `find . -name *.pyc`
#11
7
Django Extension
Note: This answer is very specific to Django project that have already been using Django Extension.
注意:这个答案非常特定于已经使用Django扩展的Django项目。
python manage.py clean_pyc
The implementation can be viewed in its source code.
实现可以在源代码中查看。
#12
6
full recursive
全递归
ll **/**/*.pyc
rm **/**/*.pyc
#13
5
Further, people usually want to remove all *.pyc
, *.pyo
files and __pycache__
directories recursively in the current directory.
此外,人们通常希望删除所有*。佩克,*。在当前目录中递归地使用pyo文件和__pycache__目录。
Command:
命令:
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
#14
3
First run:
第一次运行:
find . -type f -name "*.py[c|o]" -exec rm -f {} +
Then add:
然后添加:
export PYTHONDONTWRITEBYTECODE=1
To ~/.profile
~ / . profile
#15
3
find . -name "*.pyc"|xargs rm -rf
#16
2
rm -r
recurses into directories, but only the directories you give to rm
. It will also delete those directories. One solution is:
rm -r递归到目录中,但只返回给rm的目录。它还将删除这些目录。一个解决方案是:
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.
发现将发现所有*。在当前目录中递归地使用pyc文件,for循环将遍历找到的文件列表,删除每个文件。
#17
0
if you don't want .pyc anymore you can use this single line in a terminal:
如果你不想要。pyc你可以在终端中使用这条线:
export PYTHONDONTWRITEBYTECODE=1
if you change your mind:
如果你改变主意:
unset PYTHONDONTWRITEBYTECODE