pythonexceptionurllib2pys60

closing files properly opened with urllib2.urlopen()


I have following code in a python script

  try:
    # send the query request
    sf = urllib2.urlopen(search_query)
    search_soup = BeautifulSoup.BeautifulStoneSoup(sf.read())
    sf.close()
  except Exception, err:
    print("Couldn't get programme information.")
    print(str(err))
    return

I'm concerned because if I encounter an error on sf.read(), then sf.clsoe() is not called. I tried putting sf.close() in a finally block, but if there's an exception on urlopen() then there's no file to close and I encounter an exception in the finally block!

So then I tried

  try:
    with urllib2.urlopen(search_query) as sf:
      search_soup = BeautifulSoup.BeautifulStoneSoup(sf.read())
  except Exception, err:
    print("Couldn't get programme information.")
    print(str(err))
    return

but this raised a invalid syntax error on the with... line. How can I best handle this, I feel stupid!

As commenters have pointed out, I am using Pys60 which is python 2.5.4


Solution

  • Why not just try closing sf, and passing if it doesn't exist?

    import urllib2
    try:
        search_query = 'http://blah'
        sf = urllib2.urlopen(search_query)
        search_soup = BeautifulSoup.BeautifulStoneSoup(sf.read())
    except urllib2.URLError, err:
        print(err.reason)
    finally:
        try:
            sf.close()
        except NameError: 
            pass