diff --git a/newspaper/utils.py b/newspaper/utils.py index bfa441482..0e7ff479b 100644 --- a/newspaper/utils.py +++ b/newspaper/utils.py @@ -230,12 +230,17 @@ def inner_function(*args, **kwargs): modified = os.path.getmtime(filepath) age_seconds = time.time() - modified if age_seconds < seconds: - return pickle.load(open(filepath, "rb")) + cache_file = open(filepath, "rb") + content = pickle.load(cache_file) + cache_file.close() + return content # call the decorated function... result = function(*args, **kwargs) # ... and save the cached object for next time - pickle.dump(result, open(filepath, "wb")) + cache_file = open(filepath, "wb") + pickle.dump(result, cache_file) + cache_file.close() return result return inner_function return do_cache