title: Variable scope and Try Catch in python
body: <pre><code>import Image
  import os
  for dirname, dirs, files in os.walk("."):
      for filename in files:
          try:
              im = Image.open(os.path.join(dirname,filename))
          except IOError:
              print "error opening file :: "  + os.path.join(dirname,filename)
          print im.size
  </code></pre>
  
  <p>Here I'm trying to print the size of all the files in a directory (and sub). But I know <code>im</code> is outside the scope when in the line <code>im.size</code>. But how else do I do it without using <code>else</code> or <code>finally</code> blocks.</p>
  
  <p>The following error is shown:</p>
  
  <pre><code>Traceback (most recent call last):
    File "batch.py", line 13, in &lt;module&gt;
      print im.size
  NameError: name 'im' is not defined
  </code></pre>