/
elder247
/
aaa-python-advanced
Обзор
Документация
Войти
/
elder247
/
aaa-python-advanced
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
hw_3/task_3.py
39 строк
1019 B
Anton Trofimuk
add task_1.py, task_2.py, task_3.py
02 дек 2024, 20:44
02 дек 2024, 20:44
9dd77f4
Код
Авторство
О чём код?
import sys from functools import wraps def redirect_output(filepath: str): """ Return decorator, which redirect all function output to the file :param filepath: File to which the output will be redirected :return: Decorator """ def decorator(func): @wraps(func) def wrapper(*args, **kwargs): with open(filepath, 'w') as file: original_stdout = sys.stdout sys.stdout = file try: return func(*args, **kwargs) finally: sys.stdout.flush() sys.stdout = original_stdout return wrapper return decorator @redirect_output('./function_output.txt') def calculate(): for power in range(1, 5): for num in range(1, 20): print(num ** power, end=' ') print() if __name__ == '__main__': calculate() function_output = open('./function_output.txt', 'r') print(*function_output.readlines(), sep='')