43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
|
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()
|