diff --git a/pyop2/compilation.py b/pyop2/compilation.py index e5a9fefdd..2d72dbb11 100644 --- a/pyop2/compilation.py +++ b/pyop2/compilation.py @@ -33,6 +33,7 @@ import os +import shutil import subprocess import sys import ctypes @@ -500,31 +501,27 @@ def clear_cache(prompt=False): :arg prompt: if ``True`` prompt before removing any files """ cachedir = configuration['cache_dir'] + if not os.path.exists(cachedir): + print("Cache directory could not be found") return - - files = [os.path.join(cachedir, f) for f in os.listdir(cachedir) - if os.path.isfile(os.path.join(cachedir, f))] - nfiles = len(files) - - if nfiles == 0: + if len(os.listdir(cachedir)) == 0: print("No cached libraries to remove") return remove = True if prompt: - - user = input("Remove %d cached libraries from %s? [Y/n]: " % (nfiles, cachedir)) + user = input(f"Remove cached libraries from {cachedir}? [Y/n]: ") while user.lower() not in ['', 'y', 'n']: print("Please answer y or n.") - user = input("Remove %d cached libraries from %s? [Y/n]: " % (nfiles, cachedir)) + user = input(f"Remove cached libraries from {cachedir}? [Y/n]: ") if user.lower() == 'n': remove = False if remove: - print("Removing %d cached libraries from %s" % (nfiles, cachedir)) - [os.remove(f) for f in files] + print(f"Removing cached libraries from {cachedir}") + shutil.rmtree(cachedir) else: print("Not removing cached libraries")