Last active
April 26, 2021 21:30
-
-
Save dgrant/5159886 to your computer and use it in GitHub Desktop.
Remove all *.pyc files under the given path recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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