tMerge pull request #3276 from SomberNight/file_io_with_open - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 8c84b349b88d9e33e4d6838115770d3d17c4f89a DIR parent c90b666fd6ba2b60ccf9b52c37f9a011e8ad9eb9 HTML Author: ThomasV <thomasv@electrum.org> Date: Mon, 13 Nov 2017 09:23:21 +0100 Merge pull request #3276 from SomberNight/file_io_with_open file i/o: use 'with' keyword Diffstat: M contrib/make_locale | 13 +++++++------ M gui/qt/main_window.py | 5 ++--- M lib/bitcoin.py | 3 ++- M lib/blockchain.py | 7 +++---- M lib/exchange_rate.py | 3 ++- M lib/mnemonic.py | 3 ++- M lib/qrscanner.py | 3 ++- M lib/simple_config.py | 5 ++--- 8 files changed, 22 insertions(+), 20 deletions(-) --- DIR diff --git a/contrib/make_locale b/contrib/make_locale t@@ -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"] t@@ -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 t@@ -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') DIR diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py t@@ -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)) DIR diff --git a/lib/bitcoin.py b/lib/bitcoin.py t@@ -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 DIR diff --git a/lib/blockchain.py b/lib/blockchain.py t@@ -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): DIR diff --git a/lib/exchange_rate.py b/lib/exchange_rate.py t@@ -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 = {} DIR diff --git a/lib/mnemonic.py b/lib/mnemonic.py t@@ -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 = [] DIR diff --git a/lib/qrscanner.py b/lib/qrscanner.py t@@ -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') DIR diff --git a/lib/simple_config.py b/lib/simple_config.py t@@ -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):