Skip to contentSkip to conversation

bno_plot.py: Use `with open() as ...` context manager to automatically handle close()

PATCH New Vincent Legoll 1 participant 1 message Mar 20, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 3/10 bno_plot.py: Use `with open() as ...` context manager to automatically handle close()

---
 btt/bno_plot.py | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
btt/bno_plot.py +9 -10
index 36fc524..1a1937c 100644
@@ -98,21 +98,20 @@ if __name__ = '__main__':
 	for f in bnos:
 		t = '%s/%s' % (tmpdir, f)
 
-		fo = open(t, 'w')
-		for line in open(f, 'r'):
-			fld = line.split(None)
-			print(fld[0], fld[1], int(fld[2])-int(fld[1]), file=fo)
-		fo.close()
+		with open(t, 'w') as fo:
+			with open(f, 'r') as fi:
+				for line in fi:
+					fld = line.split(None)
+					print(fld[0], fld[1], int(fld[2])-int(fld[1]), file=fo)
 
 		t = t[t.rfind('/')+1:]
 		if plot_cmd = None: plot_cmd = "splot '%s'" % t
 		else:                plot_cmd = "%s,'%s'" % (plot_cmd, t)
 
-	fo = open('%s/plot.cmds' % tmpdir, 'w')
-	print(cmds, file=fo)
-	if len(bnos) > 10 or keys_below: print('set key below', file=fo)
-	print(plot_cmd, file=fo)
-	fo.close()
+	with open('%s/plot.cmds' % tmpdir, 'w') as fo:
+		print(cmds, file=fo)
+		if len(bnos) > 10 or keys_below: print('set key below', file=fo)
+		print(plot_cmd, file=fo)
 
 	pid = os.fork()
 	if pid = 0:
Signature · 1 line
2.20.1