From: Neal H. Walfield Date: Mon, 5 Sep 2011 20:48:02 +0000 (+0200) Subject: Make addImage more robust. X-Git-Url: https://vcs.maemo.org/git/?a=commitdiff_plain;h=a995f994a8336a24faefd3664bcbf6c58ce0b03c;hp=dff5b2a6fa115c2ad929576352a7517472fb02c1;p=feedingit Make addImage more robust. - Use with to ensure the output file handle is closed. --- diff --git a/src/rss_sqlite.py b/src/rss_sqlite.py index b08ff6a..444c918 100644 --- a/src/rss_sqlite.py +++ b/src/rss_sqlite.py @@ -24,6 +24,8 @@ # Description : Simple RSS Reader # ============================================================================ +from __future__ import with_statement + import sqlite3 from os.path import isfile, isdir from shutil import rmtree @@ -113,10 +115,12 @@ class Feed: abs_url = urljoin(baseurl,url) f = opener.open(abs_url) - outf = open(filename, "w") - outf.write(f.read()) - f.close() - outf.close() + try: + with open(filename, "w") as outf: + for data in f: + outf.write(data) + finally: + f.close() except (urllib2.HTTPError, urllib2.URLError, IOError), exception: logger.info("Could not download image %s: %s" % (abs_url, str (exception)))