From 4d5678f07907e1de464f35196c6b81b7532df985 Mon Sep 17 00:00:00 2001 From: Daniel Kotik Date: Thu, 29 Nov 2018 11:54:23 +0100 Subject: [PATCH] Add exception handling when hdf file is not found --- Laguerre_Gauss_3d/plot_2d_matplotlib.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Laguerre_Gauss_3d/plot_2d_matplotlib.py b/Laguerre_Gauss_3d/plot_2d_matplotlib.py index f9acdb9..5b14ea5 100644 --- a/Laguerre_Gauss_3d/plot_2d_matplotlib.py +++ b/Laguerre_Gauss_3d/plot_2d_matplotlib.py @@ -34,6 +34,7 @@ import numpy as np import h5py import gc +import sys def free_memory(*objects): @@ -81,13 +82,21 @@ def free_memory(*objects): filename_real = path + "e_real2_mixed-000001500.h5" # "e_real2_s-000010.00.h5" filename_imag = path + "e_imag2_mixed-000001500.h5" # "e_imag2_s-000010.00.h5" -with h5py.File(filename_real, 'r') as hf: - # print("keys: %s" % hf.keys()) - data_real = hf[list(hf.keys())[0]][:] - -with h5py.File(filename_imag, 'r') as hf: - # print("keys: %s" % hf.keys()) - data_imag = hf[list(hf.keys())[0]][:] +try: + with h5py.File(filename_real, 'r') as hf: + # print("keys: %s" % hf.keys()) + data_real = hf[list(hf.keys())[0]][:] +except EnvironmentError as e: + print(e) + sys.exit() + +try: + with h5py.File(filename_imag, 'r') as hf: + # print("keys: %s" % hf.keys()) + data_imag = hf[list(hf.keys())[0]][:] +except EnvironmentError as e: + print(e) + sys.exit() data = data_real + data_imag free_memory("data_real", "data_imag")