Skip to content

Instantly share code, notes, and snippets.

@choutianxius
Last active April 30, 2025 13:50
Show Gist options
  • Save choutianxius/4072192f24fcf71e06340d2856428cd2 to your computer and use it in GitHub Desktop.
Save choutianxius/4072192f24fcf71e06340d2856428cd2 to your computer and use it in GitHub Desktop.
Simple `pyclean` on Win/MacOS
function Show-Usage {
@"
pyclean (custom PowerShell script impl)
Description:
Recursively finds and deletes Python bytecode files (*.pyc, *.pyo) and
__pycache__ and .ipynb_checkpoints directories within the specified directories.
Usage:
pyclean [directories...]
[directories ...] One or more directories where the cleanup should be performed.
Example:
pyclean . C:\path\to\project1 C:\path\to\project2
Options:
-h, --help Display this help message and exit.
"@
}
# Check for no arguments or help flag
if ($args.Count -eq 0 -or $args[0] -eq "-h" -or $args[0] -eq "--help") {
Write-Output (Show-Usage)
exit
}
foreach ($dir in $args) {
if (Test-Path $dir -PathType Container) {
Write-Host "Cleaning Python cache files in: $dir"
# Remove Python bytecode files (*.pyc, *.pyo)
Get-ChildItem -Path $dir -Include *.pyc,*.pyo -File -Recurse -ErrorAction SilentlyContinue |
Remove-Item -Force -ErrorAction SilentlyContinue
# Remove __pycache__ directories
Get-ChildItem -Path $dir -Filter __pycache__ -Directory -Recurse -ErrorAction SilentlyContinue |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
# Remove .ipynb_checkpoints directories
Get-ChildItem -Path $dir -Filter .ipynb_checkpoints -Directory -Recurse -ErrorAction SilentlyContinue |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
}
else {
Write-Warning "$dir is not a valid directory"
}
}
#!/bin/zsh
function usage() {
cat << END
pyclean (custom shell script impl)
Description:
Recursively finds and deletes Python bytecode files (*.pyc, *.pyo),
__pycache__ directories and .ipynb_checkpoints directories within the
specified directories.
Usage:
pyclean [directories...]
[directories ...]
One or more directories where the cleanup should be performed.
Example:
pyclean /path/to/project1 /path/to/project2
Options:
-h, --help Display this help message and exit.
END
}
if [ "$#" -eq 0 ]; then
usage
exit 0
fi
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 0
fi
for dir in "$@"; do
if [ -d "$dir" ]; then
echo "Cleaning Python cache files in: $dir"
find "$dir" \
-type f -name '*.py[co]' -delete -or \
-type d -name '__pycache__' -delete -or \
-type f -path '*/.ipynb_checkpoints/*' -delete -or \
-type d -name '.ipynb_checkpoints' -delete
else
echo "Warning: $dir is not a valid directory"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment