def flush(pattern): import os, re, os.path mypath = "." for root, dirs, files in os.walk(mypath): for file in filter(lambda x: re.match(pattern, x), files): print("Remove: %s" % os.path.join(root, file)) os.remove(os.path.join(root, file)) def flush_all(): flush("^.*\.pickle$") flush("^.*\.pyc$") flush("^.*\.pyo$") if __name__ == "__main__": flush_all()