title: Getting exception details in Python
body: <p>I have to open &amp; write to about 10 different files all within the same loop.
  e.g:</p>
  
  <pre><code>for i in range(0,10):
      try:
          a=5
          file1 = open("file1.txt",'w+')
          file2 = open("file2.txt",'w+')
          #... etc
  
          print(str(a),file=file1)
          print(str(a)+"hi",file=file2)
          # ... etc
      except: 
          #error handling
  </code></pre>
  
  <p>Now what I'd like to do is be able to get specific exception information such as what file was being opened/written to within the general exception. From my current understanding, I'd have to do something like this to achieve what I want:</p>
  
  <pre><code>for i in range(0,5):
      a=5
      try:
          file1 = open("file1.txt",'w+')
          print(str(a),file=file1)
      except: 
          #error handling for file1
      try:
          file2 = open("file2.txt",'w+')
          print(str(a)+"hi",file=file2)
      except: 
          #error handling for file2
  </code></pre>
  
  <p>...Which is going to get extremely clunky and unattractive when I have to do this for about 10 different files. Is there any way to get (for example) the filename info out of a general exception like in my first example? Basically so the exception could report things like "error when writing to file1" without a try/except specifically for file1 operations.</p>
  
  <p>edit: This is a massive over-simplification of the data being written to the file. str(a) and str(a)+"hi" are not really good representations of the data actually being written; file1 may need a hardcoded integer, where file2 may need a string formatted with multiple variables. to generalize the opening/writing process into a loop isn't going to work very nicely.</p>