DAS_2024_1/haliullov_kamil_lab_2/twoapp/main.py
2024-12-29 11:43:21 +04:00

38 lines
1.2 KiB
Python

import os
def read_numbers_from_file(file_path):
if not os.path.exists(file_path):
raise FileNotFoundError(f"Файл {file_path} не найден!")
with open(file_path, 'r') as f:
lines = f.readlines()
# Получаем первое и последнее число из списка строк
first_number = int(lines[0].strip())
last_number = int(lines[-1].strip())
return first_number, last_number
def calculate_product(first_number, last_number):
product = first_number * last_number
return product
def write_result(product, output_filepath):
with open(output_filepath, 'w') as f:
f.write(str(product))
def main():
input_filepath = '/var/result/data.txt' # Исходный файл
output_filepath = '/var/result/result.txt' # Результирующий файл
try:
first_number, last_number = read_numbers_from_file(input_filepath)
product = calculate_product(first_number, last_number)
write_result(product, output_filepath)
print(f"Произведение первого и последнего числа сохранено в {output_filepath}")
except FileNotFoundError as e:
print(e)
if __name__ == "__main__":
main()