From 7df55e36da2a93ff46098a80aa3195b34f1ae525 Mon Sep 17 00:00:00 2001 From: cheeks <134818917+leftovertoast@users.noreply.github.com> Date: Thu, 6 Mar 2025 00:00:58 +0000 Subject: [PATCH] Completed 10-8 Cats and Dogs --- Chapter_10/10-08_cats_and_dogs.py | 25 +++++++++++++++++++++++++ Chapter_10/cats.txt | 3 +++ Chapter_10/dogs.txt | 3 +++ 3 files changed, 31 insertions(+) create mode 100644 Chapter_10/10-08_cats_and_dogs.py create mode 100644 Chapter_10/cats.txt create mode 100644 Chapter_10/dogs.txt diff --git a/Chapter_10/10-08_cats_and_dogs.py b/Chapter_10/10-08_cats_and_dogs.py new file mode 100644 index 0000000..3b24e93 --- /dev/null +++ b/Chapter_10/10-08_cats_and_dogs.py @@ -0,0 +1,25 @@ +# Exercise 10-8 Cats and Dogs +# Learning Objective: Read from two files and include a try except section to catch file not found error. + +from pathlib import Path + +cats = Path("./Chapter_10/cats.txt") +dogs = Path("./Chapter_10/dogs.txt") +combined_list = [] + +try: + cats_content = cats.read_text() +except FileNotFoundError: + print(f"Sorry, the file {cats} doesn't exist.") +else: + for name in cats_content.splitlines(): + combined_list.append(name) + +try: + dogs_content = dogs.read_text() +except FileNotFoundError: + print(f"Sorry, the file {dogs} doesn't exist") +else: + for name in dogs_content.splitlines(): + combined_list.append(name) +print(combined_list) \ No newline at end of file diff --git a/Chapter_10/cats.txt b/Chapter_10/cats.txt new file mode 100644 index 0000000..2524510 --- /dev/null +++ b/Chapter_10/cats.txt @@ -0,0 +1,3 @@ +Terry +Bruce +Gurt \ No newline at end of file diff --git a/Chapter_10/dogs.txt b/Chapter_10/dogs.txt new file mode 100644 index 0000000..5908f63 --- /dev/null +++ b/Chapter_10/dogs.txt @@ -0,0 +1,3 @@ +Terrance +Phil +Dingus \ No newline at end of file