DAS_2024_1/agliullov_daniyar_lab_2/service_2/main.py
2024-12-30 22:42:02 +04:00

43 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

def read_numbers_from_file(file_path):
try:
with open(file_path, 'r') as file:
# Читаем все строки и преобразуем их в числа
numbers = [float(line.strip()) for line in file.read().split() if
line.strip().isdigit() or (
line.strip().replace('.', '', 1).isdigit() and line.strip().count('.') < 2)]
return numbers
except FileNotFoundError:
print(f"Файл {file_path} не найден.")
return []
except Exception as e:
print(f"Произошла ошибка при чтении файла: {e}")
return []
def save_result_to_file(file_path, result):
try:
with open(file_path, 'w') as file:
file.write(str(result))
except Exception as e:
print(f"Произошла ошибка при записи в файл: {e}")
def main():
input_file = '/var/result/data.txt'
output_file = '/var/result/result.txt'
numbers = read_numbers_from_file(input_file)
if numbers:
first_number = numbers[0]
last_number = numbers[-1]
product = first_number * last_number
print(f"Первое число: {first_number}, Последнее число: {last_number}, Произведение: {product}")
save_result_to_file(output_file, product)
print(f"Результат сохранён в {output_file}")
else:
print("Не удалось получить числа из файла.")
if __name__ == "__main__":
main()