During my simulation, Python creates folders named __pycache__
. Not just one, but many. The __pycache__
-folders are almost always created next to the modules that are executed.
But these modules are scattered in my directory. The main folder is called LPG
and has a lot of subfolders, which in turn have further subfolders. The __pycache__
-folders can occur at all possible places.
At the end of my simulation I would like to clean up and delete all folders named __pycache__
within the LPG
-tree.
What is the best way to do this?
Currently, I am calling the function below on simulation end (also on simulation start). However, that is a bit annoying since I specifically have to write down every Path where a __pycache__
-folder might occur.
def clearCache():
"""
Removes generic `__pycache__` .
The `__pycache__` files are automatically created by python during the simulation.
This function removes the generic files on simulation start and simulation end.
"""
try:
shutil.rmtree(Path(f"{PATH_to_folder_X}/__pycache__"))
except:
pass
try:
shutil.rmtree(Path(f"{PATH_to_folder_Y}/__pycache__"))
except:
pass