프로젝트에서 모든 .pyc 파일을 제거하려면 어떻게해야합니까?
상당히 큰 프로젝트에서 일부 파일의 이름을 변경했으며 남은 .pyc 파일을 제거하고 싶습니다. bash 스크립트를 시도했습니다.
rm -r *.pyc
그러나 그것은 내가 생각했던 것처럼 폴더를 통해 반복되지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
find . -name "*.pyc" -exec rm -f {} \;
find . -name '*.pyc' -delete
확실히 가장 간단합니다.
현재 버전의 데비안 pyclean
에서는 python-minimal
패키지에 스크립트가 있습니다.
사용법은 간단합니다.
pyclean .
에 추가 ~/.bashrc
:
pyclean () {
find . -type f -name "*.py[co]" -delete
find . -type d -name "__pycache__" -delete
}
이렇게하면 모든 .pyc 및 .pyo 파일과 __pycache__
디렉토리가 제거됩니다 . 또한 매우 빠릅니다.
사용법은 간단합니다.
$ cd /path/to/directory
$ pyclean
bash> = 4.0 (또는 zsh)을 사용하는 경우
rm **/*.pyc
전체 디렉터리 트리 를 재귀 적으로 검색하는 동안 바로 첫 번째 수준의 하위 디렉터리에있는 */*.pyc
모든 .pyc
파일 을 선택합니다 **/*.pyc
. 예를 들어에 foo/bar/qux.pyc
의해 삭제 rm **/*.pyc
되지만에 의해 삭제 되지 않습니다 */*.pyc
.
globstar 셸 옵션을 활성화해야합니다. 활성화하려면 globstar
:
shopt -s globstar
상태를 확인하려면 :
shopt globstar
나는 별칭을 사용했습니다.
$ which pycclean
pycclean is aliased to `find . -name "*.pyc" | xargs -I {} rm -v "{}"'
Windows 사용자의 경우 :
del /S *.pyc
find . -name '*.pyc' -print0 | xargs -0 rm
찾기는 * .pyc 파일을 재귀 적으로 찾습니다. xargs는 해당 이름 목록을 가져와 rm으로 보냅니다. -print0 및 -0은 두 명령에 널 문자로 파일 이름을 구분하도록 지시합니다. 이렇게하면 공백이 포함 된 파일 이름과 새 줄이 포함 된 파일 이름에서도 올바르게 작동 할 수 있습니다.
-exec를 사용하는 솔루션은 작동하지만 모든 파일에 대해 rm의 새 사본을 회전시킵니다. 시스템이 느리거나 파일이 많은 경우 시간이 너무 오래 걸립니다.
인수를 몇 개 더 추가 할 수도 있습니다.
find . -iname '*.pyc' -print0 | xargs -0 --no-run-if-empty rm
iname은 * .PYC와 같이 대소 문자를 구분하지 않습니다. no-run-if-empty는 그러한 파일이없는 경우 rm에서 오류가 발생하지 않도록합니다.
$ find . -name '*.pyc' -delete
이것은보다 빠릅니다
$ 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
Just to throw another variant into the mix, you can also use backquotes like this:
rm `find . -name *.pyc`
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.
full recursive
ll **/**/*.pyc
rm **/**/*.pyc
First run:
find . -type f -name "*.py[c|o]" -exec rm -f {} +
Then add:
export PYTHONDONTWRITEBYTECODE=1
To ~/.profile
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
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 . -name "*.pyc"|xargs rm -rf
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.
참고URL : https://stackoverflow.com/questions/785519/how-do-i-remove-all-pyc-files-from-a-project
'program tip' 카테고리의 다른 글
천 단위 구분 기호로 쉼표로 숫자를 인쇄하는 방법은 무엇입니까? (0) | 2020.10.02 |
---|---|
Mac OS에서 Node.js를 최신 버전으로 업그레이드 (0) | 2020.10.02 |
일반 영어로 "git reset"은 무엇을합니까? (0) | 2020.10.02 |
Bash 스크립트에 전달 된 인수 수 확인 (0) | 2020.10.02 |
JavaScript가 비활성화되어 있는지 감지하는 방법은 무엇입니까? (0) | 2020.10.02 |