diff --git a/gramps/cli/grampscli.py b/gramps/cli/grampscli.py index ac66aa9ae..7f665d20d 100644 --- a/gramps/cli/grampscli.py +++ b/gramps/cli/grampscli.py @@ -265,9 +265,8 @@ class CLIManager(object): # Attempt to figure out the database title path = os.path.join(filename, "name.txt") try: - ifile = open(path) - title = ifile.readline().strip() - ifile.close() + with open(path) as ifile: + title = ifile.readline().strip() except: title = filename diff --git a/gramps/gen/db/generic.py b/gramps/gen/db/generic.py index e0ea382ed..55317eaf3 100644 --- a/gramps/gen/db/generic.py +++ b/gramps/gen/db/generic.py @@ -2026,9 +2026,8 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback): """ filepath = os.path.join(self._directory, "name.txt") try: - name_file = open(filepath, "r") - name = name_file.readline().strip() - name_file.close() + with open(filepath, "r") as name_file: + name = name_file.readline().strip() except (OSError, IOError) as msg: LOG.error(str(msg)) name = None @@ -2129,9 +2128,8 @@ class DbGeneric(DbWriteBase, DbReadBase, UpdateCallback, Callback): if self._directory: filepath = os.path.join(self._directory, "bdbversion.txt") try: - name_file = open(filepath, "r", encoding='utf-8') - version = name_file.readline().strip() - name_file.close() + with open(filepath, "r", encoding='utf-8') as name_file: + version = name_file.readline().strip() except (OSError, IOError) as msg: self.__log_error() version = "(0, 0, 0)" diff --git a/gramps/gen/plug/report/_book.py b/gramps/gen/plug/report/_book.py index d01548b16..8e7c405a2 100644 --- a/gramps/gen/plug/report/_book.py +++ b/gramps/gen/plug/report/_book.py @@ -527,9 +527,8 @@ class BookList(object): try: p = make_parser() p.setContentHandler(BookParser(self, self.dbase)) - the_file = open(self.file) - p.parse(the_file) - the_file.close() + with open(self.file) as the_file: + p.parse(the_file) except (IOError, OSError, ValueError, SAXParseException, KeyError, AttributeError): pass diff --git a/gramps/gen/plug/report/_paper.py b/gramps/gen/plug/report/_paper.py index 277d8a354..ce796b9f2 100644 --- a/gramps/gen/plug/report/_paper.py +++ b/gramps/gen/plug/report/_paper.py @@ -83,9 +83,8 @@ class PageSizeParser(handler.ContentHandler): try: parser = make_parser() parser.setContentHandler(PageSizeParser(paper_sizes)) - the_file = open(PAPERSIZE) - parser.parse(the_file) - the_file.close() + with open(PAPERSIZE) as the_file: + parser.parse(the_file) paper_sizes.append(PaperSize("Custom Size", -1, -1)) # always in English except (IOError, OSError, SAXParseException): paper_sizes = [ diff --git a/gramps/gui/aboutdialog.py b/gramps/gui/aboutdialog.py index c9ddcf795..fe746a014 100644 --- a/gramps/gui/aboutdialog.py +++ b/gramps/gui/aboutdialog.py @@ -96,9 +96,8 @@ class GrampsAboutDialog(Gtk.AboutDialog): self.set_artists(artists.split('\n')) try: - ifile = open(LICENSE_FILE, "r") - self.set_license(ifile.read().replace('\x0c', '')) - ifile.close() + with open(LICENSE_FILE, "r") as ifile: + self.set_license(ifile.read().replace('\x0c', '')) except IOError: self.set_license("License file is missing") @@ -214,9 +213,8 @@ def _get_authors(): parser = make_parser() parser.setContentHandler(AuthorParser(authors, contributors)) - authors_file = open(AUTHORS_FILE, encoding='utf-8') - parser.parse(authors_file) - authors_file.close() + with open(AUTHORS_FILE, encoding='utf-8') as authors_file: + parser.parse(authors_file) authors_text = [authors, contributors] diff --git a/gramps/gui/dbman.py b/gramps/gui/dbman.py index 224dc2d7a..adc7244bc 100644 --- a/gramps/gui/dbman.py +++ b/gramps/gui/dbman.py @@ -663,9 +663,8 @@ class DbManager(CLIDbManager): node = self.model.get_iter(path) filename = self.model.get_value(node, FILE_COL) try: - name_file = open(filename, "r") - file_name_to_delete=name_file.read() - name_file.close() + with open(filename, "r") as name_file: + file_name_to_delete=name_file.read() remove_filename(file_name_to_delete) directory = self.data_to_delete[1] for (top, dirs, files) in os.walk(directory): diff --git a/gramps/plugins/database/bsddb_support/read.py b/gramps/plugins/database/bsddb_support/read.py index 62bf7c02c..656edbe0d 100644 --- a/gramps/plugins/database/bsddb_support/read.py +++ b/gramps/plugins/database/bsddb_support/read.py @@ -2040,9 +2040,8 @@ class DbBsddbRead(DbReadBase, Callback): """ filepath = os.path.join(self.path, "name.txt") try: - name_file = open(filepath, "r", encoding='utf-8') - name = name_file.readline().strip() - name_file.close() + with open(filepath, "r", encoding='utf-8') as name_file: + name = name_file.readline().strip() except (OSError, IOError) as msg: self.__log_error() name = None @@ -2051,9 +2050,8 @@ class DbBsddbRead(DbReadBase, Callback): def get_version(self): filepath = os.path.join(self.path, "bdbversion.txt") try: - name_file = open(filepath, "r", encoding='utf-8') - version = name_file.readline().strip() - name_file.close() + with open(filepath, "r", encoding='utf-8') as name_file: + version = name_file.readline().strip() except (OSError, IOError) as msg: self.__log_error() version = "(0, 0, 0)" diff --git a/gramps/plugins/database/bsddb_support/summary.py b/gramps/plugins/database/bsddb_support/summary.py index 8aefd705d..4548c7525 100644 --- a/gramps/plugins/database/bsddb_support/summary.py +++ b/gramps/plugins/database/bsddb_support/summary.py @@ -16,8 +16,8 @@ def get_dbdir_summary(dirpath, name): bdbversion_file = os.path.join(dirpath, BDBVERSFN) if os.path.isfile(bdbversion_file): - vers_file = open(bdbversion_file) - bsddb_version = vers_file.readline().strip() + with open(bdbversion_file) as vers_file: + bsddb_version = vers_file.readline().strip() else: return "Unknown", "Unknown", "Unknown" diff --git a/gramps/plugins/docgen/odfdoc.py b/gramps/plugins/docgen/odfdoc.py index b3e6a9659..9aa7f567b 100644 --- a/gramps/plugins/docgen/odfdoc.py +++ b/gramps/plugins/docgen/odfdoc.py @@ -1214,9 +1214,8 @@ class ODFDoc(BaseDoc, TextDoc, DrawDoc): for image in self.media_list: try: - ifile = open(image[0], mode='rb') - self._add_zip(zfile, "Pictures/%s" % image[1], ifile.read(), t) - ifile.close() + with open(image[0], mode='rb') as ifile: + self._add_zip(zfile, "Pictures/%s" % image[1], ifile.read(), t) except: errmsg = "%s\n%s" % (_("Could not open %s") % image[0], msg) diff --git a/gramps/plugins/tool/verify.py b/gramps/plugins/tool/verify.py index 1bcb5a72f..ea9e4e475 100644 --- a/gramps/plugins/tool/verify.py +++ b/gramps/plugins/tool/verify.py @@ -572,9 +572,8 @@ class VerifyResults(ManagedWindow): def _save_ignored(self,filename): try: - f = open(filename,'wb') - pickle.dump(self.ignores, f, 1) - f.close() + with open(filename,'wb') as f: + pickle.dump(self.ignores, f, 1) return True except IOError: return False