From bf27bee3fcc5d23a08f9040adafb62dc1afcf481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0=20=D0=9A=D0=BE=D0=BF?= =?UTF-8?q?=D1=8B=D0=BB=D0=BE=D0=B2?= Date: Sun, 18 May 2025 18:30:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=81=D1=91=20=D1=81=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1week.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ data.txt | 5 ++++ output.txt | 5 ++++ 3 files changed, 88 insertions(+) create mode 100644 1week.py create mode 100644 data.txt create mode 100644 output.txt diff --git a/1week.py b/1week.py new file mode 100644 index 0000000..08dcf10 --- /dev/null +++ b/1week.py @@ -0,0 +1,78 @@ +import os + +def get_script_dir(): + """Возвращает директорию, где находится текущий скрипт""" + return os.path.dirname(os.path.abspath(__file__)) + +def create_input_file(): + """Создает исходный файл data.txt с тестовыми данными""" + script_dir = get_script_dir() + data_path = os.path.join(script_dir, 'data.txt') + + data = [ + "apples\n", + "Bananas\n", + "oranges\n", + "Grapes\n", + "watermelon\n" + ] + + try: + with open(data_path, 'w') as file: + file.writelines(data) + print(f"Файл data.txt успешно создан: {data_path}") + return True + except Exception as e: + print(f"Ошибка при создании файла: {e}") + return False + +def process_file(): + """Обрабатывает данные из файла и сохраняет результат""" + script_dir = get_script_dir() + input_path = os.path.join(script_dir, 'data.txt') + output_path = os.path.join(script_dir, 'output.txt') + + if not os.path.exists(input_path): + print(f"Файл не найден: {input_path}") + return False + + try: + with open(input_path, 'r') as file: + lines = file.readlines() + + processed_lines = [line.strip().upper() for line in lines] + processed_lines.sort() + + with open(output_path, 'w') as file: + file.writelines(line + '\n' for line in processed_lines) + + print(f"Файл output.txt успешно создан: {output_path}") + return True + except Exception as e: + print(f"Ошибка при обработке файла: {e}") + return False + +def show_results(): + """Показывает содержимое полученного файла""" + script_dir = get_script_dir() + output_path = os.path.join(script_dir, 'output.txt') + + if not os.path.exists(output_path): + print(f"Файл не найден: {output_path}") + return + + try: + with open(output_path, 'r') as file: + content = file.read() + print("\nСодержимое output.txt:") + print(content) + except Exception as e: + print(f"Ошибка при чтении файла: {e}") + +if __name__ == "__main__": + print("=== Программа обработки файлов ===") + print(f"Рабочая директория: {get_script_dir()}") + + if create_input_file(): + if process_file(): + show_results() \ No newline at end of file diff --git a/data.txt b/data.txt new file mode 100644 index 0000000..d3bc555 --- /dev/null +++ b/data.txt @@ -0,0 +1,5 @@ +apples +Bananas +oranges +Grapes +watermelon diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..597f518 --- /dev/null +++ b/output.txt @@ -0,0 +1,5 @@ +APPLES +BANANAS +GRAPES +ORANGES +WATERMELON