open files using with statement

This commit is contained in:
Cayo Puigdefabregas 2020-10-09 20:54:12 +02:00
parent 99a12b2b22
commit 49668a4f4f
2 changed files with 9 additions and 9 deletions

View File

@ -35,9 +35,9 @@ def save_json(req_json):
if not os.path.isdir(TMP_SNAPSHOTS): if not os.path.isdir(TMP_SNAPSHOTS):
os.system('mkdir -p {}'.format(TMP_SNAPSHOTS)) os.system('mkdir -p {}'.format(TMP_SNAPSHOTS))
snapshot_file = open(path_name, 'w') with open(path_name, 'w') as snapshot_file:
snapshot_file.write(json.dumps(req_json)) snapshot_file.write(json.dumps(req_json))
snapshot_file.close()
return path_name return path_name

View File

@ -492,9 +492,9 @@ def test_save_snapshot_in_file():
snapshot = {'software': '', 'version': '', 'uuid': ''} snapshot = {'software': '', 'version': '', 'uuid': ''}
if files: if files:
path_snapshot = os.path.join(TMP_SNAPSHOTS, files[0]) path_snapshot = os.path.join(TMP_SNAPSHOTS, files[0])
file_snapshot = open(path_snapshot) with open(path_snapshot) as file_snapshot:
snapshot = json.loads(file_snapshot.read()) snapshot = json.loads(file_snapshot.read())
file_snapshot.close()
os.remove(path_snapshot) os.remove(path_snapshot)
assert snapshot['software'] == snapshot_no_hid['software'] assert snapshot['software'] == snapshot_no_hid['software']
@ -515,9 +515,9 @@ def test_backup_snapshot_with_errors(user: UserClient):
files = [x for x in os.listdir(TMP_SNAPSHOTS) if uuid in x] files = [x for x in os.listdir(TMP_SNAPSHOTS) if uuid in x]
if files: if files:
path_snapshot = os.path.join(TMP_SNAPSHOTS, files[0]) path_snapshot = os.path.join(TMP_SNAPSHOTS, files[0])
file_snapshot = open(path_snapshot) with open(path_snapshot) as file_snapshot:
snapshot = json.loads(file_snapshot.read()) snapshot = json.loads(file_snapshot.read())
file_snapshot.close()
os.remove(path_snapshot) os.remove(path_snapshot)
assert snapshot['software'] == snapshot_no_hid['software'] assert snapshot['software'] == snapshot_no_hid['software']