Исправлена обработка ошибок при отсутствии файла, переход на относительные пути
This commit is contained in:
parent
838ea8d143
commit
77e1d4d3aa
16
zadanie_1.py
16
zadanie_1.py
@ -2,11 +2,8 @@ import os
|
||||
|
||||
# 1. Загрузка заказов из файла
|
||||
def load_orders(filepath: str) -> list[dict]:
|
||||
"""
|
||||
Читает файл с заказами.
|
||||
Формат строки: user_id, item_name, quantity, price_per_item
|
||||
"""
|
||||
orders = []
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as file:
|
||||
for line in file:
|
||||
line = line.strip()
|
||||
@ -15,13 +12,14 @@ def load_orders(filepath: str) -> list[dict]:
|
||||
parts = line.split(',')
|
||||
if len(parts) != 4:
|
||||
continue
|
||||
|
||||
try:
|
||||
user_id = int(parts[0].strip())
|
||||
item_name = parts[1].strip()
|
||||
quantity = int(parts[2].strip())
|
||||
price = float(parts[3].strip())
|
||||
except ValueError:
|
||||
continue
|
||||
total = quantity * price
|
||||
|
||||
orders.append({
|
||||
'user_id': user_id,
|
||||
'item_name': item_name,
|
||||
@ -29,6 +27,10 @@ def load_orders(filepath: str) -> list[dict]:
|
||||
'price': price,
|
||||
'total': total
|
||||
})
|
||||
except FileNotFoundError:
|
||||
print(f"Ошибка: файл {filepath} не найден.")
|
||||
except Exception as e:
|
||||
print(f"Ошибка при чтении файла: {e}")
|
||||
return orders
|
||||
|
||||
|
||||
@ -156,7 +158,7 @@ def save_report(filepath: str, report_text: str) -> bool:
|
||||
def main():
|
||||
print("=== Система анализа заказов интернет-магазина ===\n")
|
||||
|
||||
data_dir = r"C:\Users\Ardor\Desktop\Практика\others"
|
||||
data_dir = "data"
|
||||
|
||||
# Создаем папку, если её нет
|
||||
os.makedirs(data_dir, exist_ok=True)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user