From c65d01ea9610587c12483abfc217507adab02b6f Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sun, 12 Nov 2017 14:33:46 +0100 Subject: [PATCH] file i/o: use 'with' keyword --- contrib/make_locale | 13 +++++++------ gui/qt/main_window.py | 5 ++--- lib/bitcoin.py | 3 ++- lib/blockchain.py | 7 +++---- lib/exchange_rate.py | 3 ++- lib/mnemonic.py | 3 ++- lib/qrscanner.py | 3 ++- lib/simple_config.py | 5 ++--- 8 files changed, 22 insertions(+), 20 deletions(-) diff --git a/contrib/make_locale b/contrib/make_locale index 17aebf3e..9243a8b4 100755 --- a/contrib/make_locale +++ b/contrib/make_locale @@ -23,7 +23,8 @@ crowdin_api_key = None filename = '~/.crowdin_api_key' if os.path.exists(filename): - crowdin_api_key = open(filename).read().strip() + with open(filename) as f: + crowdin_api_key = f.read().strip() if "crowdin_api_key" in os.environ: crowdin_api_key = os.environ["crowdin_api_key"] @@ -32,8 +33,9 @@ if crowdin_api_key: # Push to Crowdin print('Push to Crowdin') url = ('https://api.crowdin.com/api/project/' + crowdin_identifier + '/update-file?key=' + crowdin_api_key) - files = {crowdin_file_name: open(locale_file_name,'rb')} - requests.request('POST', url, files=files) + with open(locale_file_name,'rb') as f: + files = {crowdin_file_name: f} + requests.request('POST', url, files=files) # Build translations print('Build translations') response = requests.request('GET', 'https://api.crowdin.com/api/project/' + crowdin_identifier + '/export?key=' + crowdin_api_key).content @@ -52,9 +54,8 @@ for name in zfobj.namelist(): if not os.path.exists(name[16:]): os.mkdir(name[16:]) else: - output = open(name[16:], 'wb') - output.write(zfobj.read(name)) - output.close() + with open(name[16:], 'wb') as output: + output.write(zfobj.read(name)) # Convert .po to .mo print('Installing') diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py index 4bad81e8..cbd29f7a 100644 --- a/gui/qt/main_window.py +++ b/gui/qt/main_window.py @@ -2235,9 +2235,8 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError): labelsFile = self.getOpenFileName(_("Open labels file"), "*.json") if not labelsFile: return try: - f = open(labelsFile, 'r') - data = f.read() - f.close() + with open(labelsFile, 'r') as f: + data = f.read() for key, value in json.loads(data).items(): self.wallet.set_label(key, value) self.show_message(_("Your labels were imported from") + " '%s'" % str(labelsFile)) diff --git a/lib/bitcoin.py b/lib/bitcoin.py index a95e0cf4..197a813d 100644 --- a/lib/bitcoin.py +++ b/lib/bitcoin.py @@ -41,7 +41,8 @@ from . import segwit_addr def read_json_dict(filename): path = os.path.join(os.path.dirname(__file__), filename) try: - r = json.loads(open(path, 'r').read()) + with open(path, 'r') as f: + r = json.loads(f.read()) except: r = {} return r diff --git a/lib/blockchain.py b/lib/blockchain.py index f8febc60..94402ac8 100644 --- a/lib/blockchain.py +++ b/lib/blockchain.py @@ -247,10 +247,9 @@ class Blockchain(util.PrintError): delta = height - self.checkpoint name = self.path() if os.path.exists(name): - f = open(name, 'rb') - f.seek(delta * 80) - h = f.read(80) - f.close() + with open(name, 'rb') as f: + f.seek(delta * 80) + h = f.read(80) return deserialize_header(h, height) def get_hash(self, height): diff --git a/lib/exchange_rate.py b/lib/exchange_rate.py index 4442e7ae..3f1cce53 100644 --- a/lib/exchange_rate.py +++ b/lib/exchange_rate.py @@ -339,7 +339,8 @@ def get_exchanges_and_currencies(): import os, json path = os.path.join(os.path.dirname(__file__), 'currencies.json') try: - return json.loads(open(path, 'r').read()) + with open(path, 'r') as f: + return json.loads(f.read()) except: pass d = {} diff --git a/lib/mnemonic.py b/lib/mnemonic.py index 6467c47e..0c9b2331 100644 --- a/lib/mnemonic.py +++ b/lib/mnemonic.py @@ -98,7 +98,8 @@ def normalize_text(seed): def load_wordlist(filename): path = os.path.join(os.path.dirname(__file__), 'wordlist', filename) - s = open(path,'r').read().strip() + with open(path, 'r') as f: + s = f.read().strip() s = unicodedata.normalize('NFKD', s) lines = s.split('\n') wordlist = [] diff --git a/lib/qrscanner.py b/lib/qrscanner.py index e231e643..f659d5e4 100644 --- a/lib/qrscanner.py +++ b/lib/qrscanner.py @@ -70,7 +70,8 @@ def _find_system_cameras(): if os.path.exists(device_root): for device in os.listdir(device_root): try: - name = open(os.path.join(device_root, device, 'name')).read() + with open(os.path.join(device_root, device, 'name')) as f: + name = f.read() except IOError: continue name = name.strip('\n') diff --git a/lib/simple_config.py b/lib/simple_config.py index c459a9bc..e1db4b2a 100644 --- a/lib/simple_config.py +++ b/lib/simple_config.py @@ -149,9 +149,8 @@ class SimpleConfig(PrintError): return path = os.path.join(self.path, "config") s = json.dumps(self.user_config, indent=4, sort_keys=True) - f = open(path, "w") - f.write(s) - f.close() + with open(path, "w") as f: + f.write(s) os.chmod(path, stat.S_IREAD | stat.S_IWRITE) def get_wallet_path(self):