всё сделано

This commit is contained in:
Данила Копылов 2025-05-18 18:30:17 +03:00
commit bf27bee3fc
3 changed files with 88 additions and 0 deletions

78
1week.py Normal file
View File

@ -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()

5
data.txt Normal file
View File

@ -0,0 +1,5 @@
apples
Bananas
oranges
Grapes
watermelon

5
output.txt Normal file
View File

@ -0,0 +1,5 @@
APPLES
BANANAS
GRAPES
ORANGES
WATERMELON