I have a program where I read in a path to a file and save it as a variable for opening and accessing the file later on. The filename can vary very slightly, in that it will be either S2A_TOA_rad_10m_atm.bsq or S2B_TOA_rad_10m_atm.bsq.
At the moment, I've set up a try-except statement which will catch for an attribute error as shown below:
try:
dataset_10m = str(dataset)+'S2A_TOA_rad_10m_atm.bsq'
dataset_20m = str(dataset)+'S2A_TOA_rad_20m_atm.bsq'
dataset_mask = str(dataset)+'S2A_TOA_rad_10m_out_hcw.bsq'
driver,metadata,EPSG_coords,wkt_start,wkt_end = get_metadata(dataset_10m)
except AttributeError:
dataset_10m = str(dataset)+'S2B_TOA_rad_10m_atm.bsq'
dataset_20m = str(dataset)+'S2B_TOA_rad_20m_atm.bsq'
dataset_mask = str(dataset)+'S2B_TOA_rad_10m_out_hcw.bsq'
driver,metadata,EPSG_coords,wkt_start,wkt_end = get_metadata(dataset_10m)
where the variable dataset is the path to the folder containing the .bsq file. This code works as I expect it to, but there is a different error message which prints when a file starting with S2B is found (even though the code itself implements properly), which I can't find a way to get rid of:
ERROR 4: /*path to folder*/S2A_TOA_rad_10m_atm.bsq: No such file or directory
This doesn't appear to be a Python error necessarily but I haven't been able to find any specifics on what this error message is or how to get rid of it. Is this the best approach to handling minor name differences in a file?
Could it be that this error is occurring later on in the try-except statement when I attempt to open the file (in the get_metadata function) which then leads to the exception clause?
EDIT
def get_metadata(filename):
'''
Runs gdalinfo on the input file and gathers the required metadata for creating the .yaml file
'''
metadata = gdal.Info(filename)
metadata = metadata.split('\n')
for line in range(len(metadata)):
if metadata[line][0:6] == 'Driver':
driver = metadata[line][8:12]
if metadata[line][0:10] == 'Upper Left':
splitline = metadata[line].split(' ')
ul_lon_EPSG = int(splitline[5][:-5])
ul_lat_EPSG = int(splitline[6][:-5])
if metadata[line][0:10] == 'Lower Left':
splitline = metadata[line].split(' ')
ll_lon_EPSG = int(splitline[5][:-5])
ll_lat_EPSG = int(splitline[6][:-5])
if metadata[line][0:11] == 'Upper Right':
splitline = metadata[line].split(' ')
ur_lon_EPSG = int(splitline[4][:-5])
ur_lat_EPSG = int(splitline[5][:-5])
if metadata[line][0:11] == 'Lower Right':
splitline = metadata[line].split(' ')
lr_lon_EPSG = int(splitline[4][:-5])
lr_lat_EPSG = int(splitline[5][:-5])
if metadata[line][0:21] == 'Coordinate System is:':
wkt_start = line+1
if metadata[line][0:6] == 'Origin':
wkt_end = line
EPSG_coords = [[ll_lon_EPSG,ll_lat_EPSG],[lr_lon_EPSG,lr_lat_EPSG],[ul_lon_EPSG,ul_lat_EPSG],[ur_lon_EPSG,ur_lat_EPSG]]
return driver,metadata,EPSG_coords,wkt_start,wkt_end