From b45fcea61598f6ccc2ed4e38e3de3e594efda02c Mon Sep 17 00:00:00 2001 From: lixiaoyong1 Date: Tue, 10 Dec 2024 10:24:23 -0500 Subject: [PATCH] Add exception handling mechanism for function if_str_in_file --- common/file.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/common/file.py b/common/file.py index c086954..d0a2d55 100644 --- a/common/file.py +++ b/common/file.py @@ -57,8 +57,18 @@ class FileOperation: @staticmethod def if_str_in_file(file_name, dist_str): - with open(file_name, 'r') as file: - return any(dist_str in line for line in file) + i = 0 + try: + with io.open(file_name,'r') as file: + for item in file.readlines(): + if dist_str in str(item): + i = i + 1 + if i != 0: + return True + else: + return False + except FileNotFoundError: + return False @staticmethod def get_str_num_in_file(file_name, dist_str): -- Gitee