Исправлена обработка ошибок при отсутствии файла, переход на относительные пути

This commit is contained in:
Антон Репин 2026-05-07 13:52:33 +03:00
parent 838ea8d143
commit 77e1d4d3aa

View File

@ -2,33 +2,35 @@ import os
# 1. Загрузка заказов из файла # 1. Загрузка заказов из файла
def load_orders(filepath: str) -> list[dict]: def load_orders(filepath: str) -> list[dict]:
"""
Читает файл с заказами.
Формат строки: user_id, item_name, quantity, price_per_item
"""
orders = [] orders = []
with open(filepath, 'r', encoding='utf-8') as file: try:
for line in file: with open(filepath, 'r', encoding='utf-8') as file:
line = line.strip() for line in file:
if not line: line = line.strip()
continue if not line:
parts = line.split(',') continue
if len(parts) != 4: parts = line.split(',')
continue if len(parts) != 4:
continue
user_id = int(parts[0].strip()) try:
item_name = parts[1].strip() user_id = int(parts[0].strip())
quantity = int(parts[2].strip()) item_name = parts[1].strip()
price = float(parts[3].strip()) quantity = int(parts[2].strip())
total = quantity * price price = float(parts[3].strip())
except ValueError:
orders.append({ continue
'user_id': user_id, total = quantity * price
'item_name': item_name, orders.append({
'quantity': quantity, 'user_id': user_id,
'price': price, 'item_name': item_name,
'total': total 'quantity': quantity,
}) 'price': price,
'total': total
})
except FileNotFoundError:
print(f"Ошибка: файл {filepath} не найден.")
except Exception as e:
print(f"Ошибка при чтении файла: {e}")
return orders return orders
@ -156,7 +158,7 @@ def save_report(filepath: str, report_text: str) -> bool:
def main(): def main():
print("=== Система анализа заказов интернет-магазина ===\n") print("=== Система анализа заказов интернет-магазина ===\n")
data_dir = r"C:\Users\Ardor\Desktop\Практика\others" data_dir = "data"
# Создаем папку, если её нет # Создаем папку, если её нет
os.makedirs(data_dir, exist_ok=True) os.makedirs(data_dir, exist_ok=True)