diff --git a/pysollib/actions.py b/pysollib/actions.py index 559d728a..6f135482 100644 --- a/pysollib/actions.py +++ b/pysollib/actions.py @@ -519,7 +519,8 @@ class PysolMenubar(PysolMenubarTk): text += os.linesep enc = locale.getpreferredencoding() try: - open(fn, 'a').write(text.encode(enc, 'replace')) + with open(fn, 'at') as fh: + fh.write(text.encode(enc, 'replace')) except Exception as err: d = MfxExceptionDialog( self.top, err, diff --git a/pysollib/cardsetparser.py b/pysollib/cardsetparser.py index 65a698f1..c96634f7 100644 --- a/pysollib/cardsetparser.py +++ b/pysollib/cardsetparser.py @@ -32,7 +32,7 @@ def read_cardset_config(dirname, filename): This function returns None if any errors occurred. """ - with open(filename, "r") as f: + with open(filename, "rt") as f: lines = f.readlines() lines = [line.strip() for line in lines] if not lines[0].startswith("PySol"): diff --git a/pysollib/htmllib2.py b/pysollib/htmllib2.py index 1e705ac8..7ff6b2a9 100644 --- a/pysollib/htmllib2.py +++ b/pysollib/htmllib2.py @@ -498,19 +498,15 @@ def test(args=None): fn = 'test.html' if fn == '-': - f = sys.stdin + data = sys.stdin.read() else: try: - f = open(fn, 'r') + with open(fn, 'rt') as fh: + data = fh.read() except IOError as msg: print(fn, ":", msg) sys.exit(1) - data = f.read() - - if f is not sys.stdin: - f.close() - if silent: f = formatter.NullFormatter() else: diff --git a/pysollib/mfxutil.py b/pysollib/mfxutil.py index a8b48365..b5b27ad4 100644 --- a/pysollib/mfxutil.py +++ b/pysollib/mfxutil.py @@ -285,30 +285,23 @@ class KwStruct: # ************************************************************************ def pickle(obj, filename, protocol=0): - f = None try: - f = open(filename, "wb") - Pickler(f, protocol).dump(obj) - f.close() - f = None + with open(filename, "wb") as fh: + Pickler(fh, protocol).dump(obj) # print "Pickled", filename finally: - if f: - f.close() + pass def unpickle(filename): - f, obj = None, None + obj = None try: - f = open(filename, "rb") - x = Unpickler(f).load() - f.close() - f = None + with open(filename, "rb") as fh: + x = Unpickler(fh).load() obj = x # print "Unpickled", filename finally: - if f: - f.close() + pass return obj diff --git a/pysollib/tile/selectcardset.py b/pysollib/tile/selectcardset.py index 45b45f41..4f0e55ca 100644 --- a/pysollib/tile/selectcardset.py +++ b/pysollib/tile/selectcardset.py @@ -524,9 +524,10 @@ class CardsetInfoDialog(MfxDialog): frame.rowconfigure(1, weight=1) # text = '' - f = os.path.join(cardset.dir, "COPYRIGHT") + fn = os.path.join(cardset.dir, "COPYRIGHT") try: - text = open(f).read() + with open(fn, "rt") as fh: + text = fh.read() except Exception: pass if text: diff --git a/pysollib/tk/selectcardset.py b/pysollib/tk/selectcardset.py index 623ed23d..0f0ff27c 100644 --- a/pysollib/tk/selectcardset.py +++ b/pysollib/tk/selectcardset.py @@ -487,9 +487,10 @@ class CardsetInfoDialog(MfxDialog): frame.rowconfigure(1, weight=1) # text = '' - f = os.path.join(cardset.dir, "COPYRIGHT") + fn = os.path.join(cardset.dir, "COPYRIGHT") try: - text = open(f).read() + with open(fn, "rt") as fh: + text = fh.read() except Exception: pass if text: diff --git a/pysollib/ui/tktile/tkhtml.py b/pysollib/ui/tktile/tkhtml.py index b15b9597..4e31b62d 100644 --- a/pysollib/ui/tktile/tkhtml.py +++ b/pysollib/ui/tktile/tkhtml.py @@ -295,7 +295,8 @@ class Base_HTMLViewer: import codecs return codecs.open(url, encoding='utf-8') else: - return open(url, "rb") + with open(url, "rb") as fh: + return fh return my_open(url), url def display(self, url, add=1, relpath=1, xview=0, yview=0):