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'
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')

View File

@ -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))

View File

@ -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

View File

@ -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):

View File

@ -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 = {}

View File

@ -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 = []

View File

@ -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')

View File

@ -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):