在我的模拟过程中,Python会创建名为__pycache__
的文件夹。不仅是一个,而是很多个。这些__pycache__
文件夹几乎总是在执行的模块旁边创建。
但是这些模块在我的目录中是分散的。主文件夹名为LPG
,有很多子文件夹,这些子文件夹又有进一步的子文件夹。这些__pycache__
文件夹可能出现在所有可能的位置。
在模拟结束时,我想清理并删除LPG
-树内所有名为__pycache__
的文件夹。
最好的做法是什么?
目前,我在模拟结束时(也在模拟开始时)调用下面的函数。然而,这有点麻烦,因为我必须明确写下每个可能出现__pycache__
文件夹的路径。
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