file i/o: use 'with' keyword

This commit is contained in:
SomberNight 2017-11-12 14:33:46 +01:00
parent 8fcf1656d3
commit c65d01ea96
8 changed files with 22 additions and 20 deletions

View File

@ -23,7 +23,8 @@ crowdin_api_key = None
filename = '~/.crowdin_api_key' filename = '~/.crowdin_api_key'
if os.path.exists(filename): 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: if "crowdin_api_key" in os.environ:
crowdin_api_key = os.environ["crowdin_api_key"] crowdin_api_key = os.environ["crowdin_api_key"]
@ -32,8 +33,9 @@ if crowdin_api_key:
# Push to Crowdin # Push to Crowdin
print('Push to Crowdin') print('Push to Crowdin')
url = ('https://api.crowdin.com/api/project/' + crowdin_identifier + '/update-file?key=' + crowdin_api_key) url = ('https://api.crowdin.com/api/project/' + crowdin_identifier + '/update-file?key=' + crowdin_api_key)
files = {crowdin_file_name: open(locale_file_name,'rb')} with open(locale_file_name,'rb') as f:
requests.request('POST', url, files=files) files = {crowdin_file_name: f}
requests.request('POST', url, files=files)
# Build translations # Build translations
print('Build translations') print('Build translations')
response = requests.request('GET', 'https://api.crowdin.com/api/project/' + crowdin_identifier + '/export?key=' + crowdin_api_key).content 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:]): if not os.path.exists(name[16:]):
os.mkdir(name[16:]) os.mkdir(name[16:])
else: else:
output = open(name[16:], 'wb') with open(name[16:], 'wb') as output:
output.write(zfobj.read(name)) output.write(zfobj.read(name))
output.close()
# Convert .po to .mo # Convert .po to .mo
print('Installing') print('Installing')

View File

@ -2235,9 +2235,8 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
labelsFile = self.getOpenFileName(_("Open labels file"), "*.json") labelsFile = self.getOpenFileName(_("Open labels file"), "*.json")
if not labelsFile: return if not labelsFile: return
try: try:
f = open(labelsFile, 'r') with open(labelsFile, 'r') as f:
data = f.read() data = f.read()
f.close()
for key, value in json.loads(data).items(): for key, value in json.loads(data).items():
self.wallet.set_label(key, value) self.wallet.set_label(key, value)
self.show_message(_("Your labels were imported from") + " '%s'" % str(labelsFile)) self.show_message(_("Your labels were imported from") + " '%s'" % str(labelsFile))

View File

@ -41,7 +41,8 @@ from . import segwit_addr
def read_json_dict(filename): def read_json_dict(filename):
path = os.path.join(os.path.dirname(__file__), filename) path = os.path.join(os.path.dirname(__file__), filename)
try: try:
r = json.loads(open(path, 'r').read()) with open(path, 'r') as f:
r = json.loads(f.read())
except: except:
r = {} r = {}
return r return r

View File

@ -247,10 +247,9 @@ class Blockchain(util.PrintError):
delta = height - self.checkpoint delta = height - self.checkpoint
name = self.path() name = self.path()
if os.path.exists(name): if os.path.exists(name):
f = open(name, 'rb') with open(name, 'rb') as f:
f.seek(delta * 80) f.seek(delta * 80)
h = f.read(80) h = f.read(80)
f.close()
return deserialize_header(h, height) return deserialize_header(h, height)
def get_hash(self, height): def get_hash(self, height):

View File

@ -339,7 +339,8 @@ def get_exchanges_and_currencies():
import os, json import os, json
path = os.path.join(os.path.dirname(__file__), 'currencies.json') path = os.path.join(os.path.dirname(__file__), 'currencies.json')
try: try:
return json.loads(open(path, 'r').read()) with open(path, 'r') as f:
return json.loads(f.read())
except: except:
pass pass
d = {} d = {}

View File

@ -98,7 +98,8 @@ def normalize_text(seed):
def load_wordlist(filename): def load_wordlist(filename):
path = os.path.join(os.path.dirname(__file__), '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) s = unicodedata.normalize('NFKD', s)
lines = s.split('\n') lines = s.split('\n')
wordlist = [] wordlist = []

View File

@ -70,7 +70,8 @@ def _find_system_cameras():
if os.path.exists(device_root): if os.path.exists(device_root):
for device in os.listdir(device_root): for device in os.listdir(device_root):
try: 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: except IOError:
continue continue
name = name.strip('\n') name = name.strip('\n')

View File

@ -149,9 +149,8 @@ class SimpleConfig(PrintError):
return return
path = os.path.join(self.path, "config") path = os.path.join(self.path, "config")
s = json.dumps(self.user_config, indent=4, sort_keys=True) s = json.dumps(self.user_config, indent=4, sort_keys=True)
f = open(path, "w") with open(path, "w") as f:
f.write(s) f.write(s)
f.close()
os.chmod(path, stat.S_IREAD | stat.S_IWRITE) os.chmod(path, stat.S_IREAD | stat.S_IWRITE)
def get_wallet_path(self): def get_wallet_path(self):