I had the following idea. Is it possible to implement a retry routine in python? Here is a simple example of what I have done. I would like to have a more flexible solution. Independent of the function. So switch removeFile with any other function and get rid of the while loop in the main.
import os
import time
def removeFile(file):
    try:
        os.remove(file)
        print("removed : "+file)
        return True
    except PermissionError:
        print("could not delete file "+file+" ; will try again")
        return False
if __name__ == "__main__":
    file = "some_path/file.ext"
    sucess = False
    maxCount = 5
    count = 0
    while not sucess:
        sucess = removeFile(file)
        count += 1
        if count == maxCount:
            sucess = True
            print("could not delete file "+file+" ; permission denied.")
        time.sleep(5)
 
    