forked from Alexey/DAS_2024_1
60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
|
# Путь к файлу для чтения
|
||
|
DATA_FILE = "/var/data/data.txt"
|
||
|
|
||
|
# Путь к файлу для записи
|
||
|
RESULT_FILE = "/var/result/result.txt"
|
||
|
|
||
|
|
||
|
def find_biggest_number(data_file):
|
||
|
"""Поиск наибольшего числа в файле"""
|
||
|
|
||
|
try:
|
||
|
with open(data_file, "r") as f:
|
||
|
numbers = [int(line.strip()) for line in f.readlines()]
|
||
|
|
||
|
biggest_num = max(numbers)
|
||
|
return biggest_num
|
||
|
|
||
|
except ValueError as e:
|
||
|
print(f"Ошибка преобразования строки: {e}")
|
||
|
|
||
|
except Exception as e:
|
||
|
print(f"Ошибка чтения файла '{data_file}': {e}")
|
||
|
|
||
|
return None
|
||
|
|
||
|
|
||
|
def square_number(num):
|
||
|
"""Возвращает квадрат числа"""
|
||
|
|
||
|
return num**2
|
||
|
|
||
|
|
||
|
def write_result(result_file, result):
|
||
|
"""Фиксирует результат в файл."""
|
||
|
|
||
|
try:
|
||
|
with open(result_file, "w") as f:
|
||
|
f.write(str(result))
|
||
|
|
||
|
print(f"Результат '{result}' успешно передан в '{result_file}'.")
|
||
|
|
||
|
except Exception as e:
|
||
|
print(f"Ошибка фиксации результата в файл '{result_file}': {e}")
|
||
|
|
||
|
|
||
|
def main():
|
||
|
biggest_num = find_biggest_number(DATA_FILE)
|
||
|
|
||
|
if biggest_num is not None:
|
||
|
result = square_number(biggest_num)
|
||
|
|
||
|
write_result(RESULT_FILE, result)
|
||
|
print(result)
|
||
|
|
||
|
else:
|
||
|
print("Нет чисел в файле.")
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|