Advertisement
ksieradzinski

Untitled

Jun 4th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import csv
  2. import os
  3.  
  4. COLUMNS = ['name', 'is_done']
  5.  
  6. def add_todo(name: str):
  7. file_exists = os.path.exists("todos.csv")
  8. with open("todos.csv", "a", encoding="utf8", newline="") as file:
  9. writer = csv.DictWriter(file, fieldnames=COLUMNS)
  10. if not file_exists:
  11. writer.writeheader()
  12.  
  13. writer.writerow({
  14. "name": name,
  15. "is_done": 0
  16. })
  17.  
  18.  
  19. def get_todos():
  20. with open("todos.csv", encoding="utf8", newline="") as file:
  21. reader = csv.DictReader(file)
  22.  
  23. print("Zadania do zrobienia: ")
  24. for row_id, row in enumerate(reader, start=1):
  25. if row['is_done'] == "0":
  26. print("❌", end="")
  27. else:
  28. print("✅", end="")
  29.  
  30. print(f" {row_id}: {row['name']}")
  31.  
  32.  
  33. def toggle_status(row_id_to_change: int):
  34. todos = []
  35.  
  36. with open("todos.csv", encoding="utf8", newline="") as file:
  37. reader = csv.DictReader(file)
  38. for row_id, row in enumerate(reader, start=1):
  39. if row_id_to_change == row_id:
  40. if row['is_done'] == "0":
  41. row['is_done'] = "1"
  42. else:
  43. row['is_done'] = "0"
  44.  
  45. todos.append(row)
  46.  
  47. with open("todos.csv", "w", encoding="utf8", newline="") as file:
  48. writer = csv.DictWriter(file, fieldnames=COLUMNS)
  49. writer.writeheader()
  50. writer.writerows(todos)
  51.  
  52.  
  53. get_todos()
  54. action = None
  55. while action not in ["d", "z"]:
  56. action = input("Co chcesz zrobić? [d] - dodaj, [z] - zmień")
  57. if action == "d":
  58. name = input("Co masz do zrobienia? ")
  59. add_todo(name)
  60. else:
  61. todo_id = input("Które todo chcesz zmienić? ")
  62. toggle_status(int(todo_id))
  63. get_todos()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement