[bugfix] File wasn't closed explicitly, add implicit close by with statement

This commit is contained in:
Darius Arnold 2018-11-30 11:20:47 +01:00
parent 6afb61b666
commit 3abeabc75c

View File

@ -3475,43 +3475,43 @@ class Project(object):
File structure (each row): event, date, time, magnitude, latitude, longitude, depth File structure (each row): event, date, time, magnitude, latitude, longitude, depth
separated by :param:separator each. separated by :param:separator each.
''' '''
infile = open(filename, 'r') with open(filename, 'r') as infile:
for line in infile.readlines(): for line in infile.readlines():
eventID, date, time, mag, lat, lon, depth = line.split(separator)[:7] eventID, date, time, mag, lat, lon, depth = line.split(separator)[:7]
# skip first line # skip first line
try: try:
day, month, year = date.split('/') day, month, year = date.split('/')
except: except:
continue continue
year = int(year) year = int(year)
# hardcoded, if year only consists of 2 digits (e.g. 16 instead of 2016) # hardcoded, if year only consists of 2 digits (e.g. 16 instead of 2016)
if year < 100: if year < 100:
year += 2000 year += 2000
datetime = '{}-{}-{}T{}'.format(year, month, day, time) datetime = '{}-{}-{}T{}'.format(year, month, day, time)
try: try:
datetime = UTCDateTime(datetime) datetime = UTCDateTime(datetime)
except Exception as e: except Exception as e:
print(e, datetime, filename) print(e, datetime, filename)
continue continue
for event in self.eventlist: for event in self.eventlist:
if eventID in str(event.resource_id) or eventID in event.origins: if eventID in str(event.resource_id) or eventID in event.origins:
if event.origins: if event.origins:
origin = event.origins[0] # should have only one origin origin = event.origins[0] # should have only one origin
if origin.time == datetime: if origin.time == datetime:
origin.latitude = float(lat) origin.latitude = float(lat)
origin.longitude = float(lon) origin.longitude = float(lon)
origin.depth = float(depth) origin.depth = float(depth)
else: else:
continue continue
elif not event.origins: elif not event.origins:
origin = Origin(resource_id=event.resource_id, origin = Origin(resource_id=event.resource_id,
time=datetime, latitude=float(lat), time=datetime, latitude=float(lat),
longitude=float(lon), depth=float(depth)) longitude=float(lon), depth=float(depth))
event.origins.append(origin) event.origins.append(origin)
event.magnitudes.append(Magnitude(resource_id=event.resource_id, event.magnitudes.append(Magnitude(resource_id=event.resource_id,
mag=float(mag), mag=float(mag),
mag_type='M')) mag_type='M'))
break break
def search_eventfile_info(self): def search_eventfile_info(self):
''' '''