From 1c59b4873a025fa509e86ab0b027d10a75f7a9f6 Mon Sep 17 00:00:00 2001 From: lixiaoyong1 Date: Tue, 10 Dec 2024 14:05:44 -0500 Subject: [PATCH] Add exception handling mechanism for function get_str_num_in_file --- common/file.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/common/file.py b/common/file.py index d0a2d55..fb871f7 100644 --- a/common/file.py +++ b/common/file.py @@ -74,12 +74,15 @@ class FileOperation: def get_str_num_in_file(file_name, dist_str): i = 1 dist_list = [] - with io.open(file_name,'r') as file: - for item in file.readlines(): - if dist_str in item: - dist_list.append(i) - i = i + 1 - return dist_list + try: + with io.open(file_name,'r') as file: + for item in file.readlines(): + if dist_str in str(item): + dist_list.append(i) + i = i + 1 + return dist_list + except FileNotFoundError: + return [] @staticmethod def readfile(filename): -- Gitee