program tip

프로젝트에서 모든 .pyc 파일을 제거하려면 어떻게해야합니까?

radiobox 2020. 10. 2. 21:57
반응형

프로젝트에서 모든 .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

반응형