Use 'with' keyword for file reading

This is applied to /software and /hardware file reads.
We should use 'with' instead of opening and closing a file since this prevents
that files stay open after an exception is raised.
more_events
Roberto Hueso Gómez 2020-02-05 13:41:34 +01:00 committed by Alvaro Neira Ayuso
parent 1c236b4548
commit 1fd9f2e07c
1 changed files with 4 additions and 9 deletions

View File

@ -114,11 +114,8 @@ class ogThread():
jsonResp = jsonResponse()
jsonResp.addElement('partition', request.getPartition())
f = open(path, "r")
output = f.read()
f.close()
jsonResp.addElement('software', output)
with open(path, 'r') as f:
jsonResp.addElement('software', f.read())
response = restResponse(ogResponses.OK, jsonResp)
client.send(response.get())
@ -132,10 +129,8 @@ class ogThread():
return
jsonResp = jsonResponse()
f = open(path, "r")
text = f.read()
f.close()
jsonResp.addElement('hardware', text)
with open(path, 'r') as f:
jsonResp.addElement('hardware', f.read())
response = restResponse(ogResponses.OK, jsonResp)
client.send(response.get())