Python script to write exception.KeyError to text file
<h2>I have nested dictionary as following:</h2>

<pre><code>Dict1 | level 1-1  | level 2-1-1 | level 3-1-1-1 | core [1]
                                                 | core [2]
                                                 | core [3]
                                 | level 3-1-1-2 | core [1]
                                                 | core [2]
                   | level 2-1-2 | level 3-1-2-1 | core [1]
                                                 | core [2]
                                 | level 3-1-2-2 | core [1]
      | level 1-2  | level 2-2-1 | level 3-2-1-1 | core [1]
                                 | level 3-2-1-2 | core [1]
                   | level 2-2-2 | level 3-2-2-1 | core [1]
                                                 | core [2]
                                 | level 3-2-2-2 | core [1]
</code></pre>

<h2>  ..........</h2>

<p>level 1 would be my <strong>key</strong>
level 2 would be my <strong>key1</strong>
level 3 would be my <strong>key2</strong>
core would be my <strong>value2</strong> for key2
core and level 3 would be my <strong>value1</strong> for key1
core, level 3, and level 2 would be my <strong>value</strong> for key</p>

<p>When the script was executed, there were some errors, specifically KeyError of <strong>key2</strong>.  I came up with couple lines to put this exception to a log.txt file.  ie:</p>

<pre><code>except KeyError:
  save_to_log (time_stamp, sys.exc_info()[0], sys.exc_info()[1])
  continue
</code></pre>

<p>However, it can only output the <strong>key2</strong> information.  I would like to be able to output the <strong>key</strong> information in respect to the <strong>key2</strong> so that I can easily identify the fault in the original file.  Is this possible in Python?  Any suggestion is greatly appreciated.  Thanks in advance.</p>
