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)) if __name__ == "__main__": flush("^.*\.pickle$") flush("^.*\.pyc$") flush("^.*\.pyo$")