From e7a03ef0a4193d315807d592b88ec9a7423a4086 Mon Sep 17 00:00:00 2001 From: TeachMeTW Date: Mon, 16 Dec 2024 11:50:57 -0800 Subject: [PATCH] Fix file handling to prevent ValueError and terminal crashes - Replaced manual file open/close with `with` statement to ensure proper resource management. - Removed redundant file read operation after the file was closed. - Resolved `ValueError: I/O operation on closed file` and addressed terminal crashing issue during execution. --- .../net/ext_service/transit_matching/match_stops.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/emission/net/ext_service/transit_matching/match_stops.py b/emission/net/ext_service/transit_matching/match_stops.py index afa5d6abd..3e6deb1c3 100644 --- a/emission/net/ext_service/transit_matching/match_stops.py +++ b/emission/net/ext_service/transit_matching/match_stops.py @@ -15,12 +15,12 @@ url = "https://lz4.overpass-api.de/" try: - query_file = open('conf/net/ext_service/overpass_transit_stops_query_template') -except: + with open('conf/net/ext_service/overpass_transit_stops_query_template', 'r', encoding='UTF-8') as query_file: + query_string = "".join(query_file.readlines()) +except FileNotFoundError: print("transit stops query not configured, falling back to default") - query_file = open('conf/net/ext_service/overpass_transit_stops_query_template.sample') - -query_string = "".join(query_file.readlines()) + with open('conf/net/ext_service/overpass_transit_stops_query_template.sample', 'r', encoding='UTF-8') as query_file: + query_string = "".join(query_file.readlines()) RETRY = -1