Clean way to handle file not found exception BACKGROUND
I have a function where i cache file from aws s3. I provide it the path on s3 and also the local path of where i want the cached file to live. Sometimes if the file does not exist on s3 it will return a 404 error. That makes sense but i want to handle it gracefully. What i want to do is if the file i am trying to cache does not exist I want my get_overview_stream function to return None.
CODE
def get_overview_stream(*, environment, proxy_key):
    s3_overview_file_path = f&quot;s3://{FOO_TRACK_BUCKET}/{environment}&quot;
    overview_file = f&quot;{proxy_key}.csv&quot;
    local_path = cache_directory(environment)
    try:
        cache.cache_file(s3_overview_file_path, local_path, overview_file)
        overview_file_cache = local_path / f&quot;{proxy_key}.csv&quot;
        return overview_file_cache.open(&quot;r&quot;)
    except botocore.exceptions.ClientError as e:
        if e.response[&quot;Error&quot;][&quot;Code&quot;] == &quot;404&quot;:
            return None
        else:
            raise

Attempted Solution Issue
I am very new to python so i am not sure if this the best way to handle this exception and if there is a more cleaner way. If so i would love to hear feedback.
I would suggest not opening the file in your function but instead returning the path or None, such that the caller can open the file using a with statement.