Completed 10-9 Silent Cats and Dogs
This commit is contained in:
parent
7df55e36da
commit
c7cb0c5bde
27
Chapter_10/10-09_silent_cats_and_dogs.py
Normal file
27
Chapter_10/10-09_silent_cats_and_dogs.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# Exercise 10-9 Silent Cats and Dogs
|
||||||
|
# Learning Objective: Read from two files and include a try except section to catch file not found error this time, however, make it fail silently.
|
||||||
|
|
||||||
|
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:
|
||||||
|
pass
|
||||||
|
#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:
|
||||||
|
pass
|
||||||
|
#print(f"Sorry, the file {dogs} doesn't exist")
|
||||||
|
else:
|
||||||
|
for name in dogs_content.splitlines():
|
||||||
|
combined_list.append(name)
|
||||||
|
print(combined_list)
|
||||||
Loading…
x
Reference in New Issue
Block a user