Skip to content

Instantly share code, notes, and snippets.

@dgrant
Last active April 26, 2021 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgrant/5159886 to your computer and use it in GitHub Desktop.
Save dgrant/5159886 to your computer and use it in GitHub Desktop.
Remove all *.pyc files under the given path recursively
#!/usr/bin/env python
"""
Remove all .pyc files in the path recursively
"""
from __future__ import print_function
import argparse
import os
import shutil
def remove_pyc(path):
"""
Remove all .pyc files in the path recursively
"""
for root, dirs, files in os.walk(path):
for dir in dirs:
if dir == '__pycache__':
fullpath = os.path.join(root, dir)
print("removing", fullpath)
shutil.rmtree(fullpath)
for afile in files:
fullpath = os.path.join(root, afile)
if os.path.splitext(afile)[1] == ".pyc":
print("removing", fullpath)
os.remove(fullpath)
def main():
""" main script """
parser = argparse.ArgumentParser()
parser.add_argument('path', metavar='PATH',
help='Path to delete .pyc files from')
args = parser.parse_args()
remove_pyc(args.path)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment