Completed 10-8 Cats and Dogs
This commit is contained in:
parent
08bfce4c0a
commit
d70f69744d
25
Chapter_10/10-8_cats_and_dogs.py
Normal file
25
Chapter_10/10-8_cats_and_dogs.py
Normal file
@ -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)
|
||||
3
Chapter_10/cats.txt
Normal file
3
Chapter_10/cats.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Terry
|
||||
Jones
|
||||
Spooter
|
||||
3
Chapter_10/dogs.txt
Normal file
3
Chapter_10/dogs.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Frank
|
||||
Chonk
|
||||
Gonzo
|
||||
Loading…
x
Reference in New Issue
Block a user