修改你在练习10-8中编写的except 代码块,让程序在文件不存在时一言不发。
def write_list(filename, tables):
with open(filename, 'a') as f1:
for table in tables:
f1.write(table+"\n")
cats = ['katte', 'bai', 'ming', 'hei']
dogs = ['xiao', 'lei', 'le']
write_list('cats.txt', cats)
write_list('dogs.txt', dogs)
def read_file(filename):
try:
with open(filename) as file_object:
content = file_object.read()
except FileNotFoundError:
pass
else:
print('The content of '+filename+" is:")
print(content.strip()+'\n')
names = ['cats.txt', 'dogs.txt', 'birds.txt']
for name in names:
read_file(name)