It seems like a trivial issue but I've noticed lately that the shutil.rmtree function is either taking a ridiculously long time (like 6 hours) or is simply blocking the execution of the remaining script.
I have a script that does a bunch of operations but before starting everything else, it deletes this folder called "temp" and waits for 5 minutes, then it does a bunch of operations. I've noticed that recently the script is taking over 6 hours to run instead of the usual 10 minutes. After troubleshooting the issue to looks like the script is getting stuck on the shutil.rmtree function which basically blocks further execution of the script. I know the script is logically and programmatically fine but this delay is simply causing a lot of trouble. Below is the sample of the script.
import shutil
from pathlib import Path
import os
def delFolder(folderPath):
try:
shutil.rmtree(folderPath)
except:
pass
def main_func(modulePath):
modulePath = Path(modulePath)
# Variable declarations and other stuff
temp_folder = Path.joinpath(modulePath, "tempFolder")
print("Deleting temp folder....")
delFolder(temp_folder )
print("Sleeping for 5 min....")
time.sleep(5*60)
# Other script functionality to be executed.
if __name__ == "__main__":
this_modulePath = os.getcwd()
main_func(this_modulePath)
It takes forever(like over 6 hours) to reach from Deleting temp folder.... to Sleeping for 5 min..... I don't why this is happening with shutil. The folder that I am trying to delete is around 4 GB in size and has many files and subfolders in it and no other program is using any files from the temp folder (except onedrive sync, which shouldn't be an issue)