10-8
def count_words(filename):
try:
with open(filename) as f:
c=f.read()
except FileNotFoundError:
print(f"Sorry,the file {filename} did not exist.")
else:
print(c)
filenames=['cats.txt','dogs.txt']
for filename in filenames:
filename.hanshu() #记错了在类中使用句点表示法,表示为instance.methods()
#在函数中,调用函数的方法是函数名()即可
count_words(filename)
10-9
def method(filename):
try:
with open(filename) as f:
c=f.read()
except FileNotFoundError:
pass
else:
print(c)
filenames=['cats.txt','dogs.txt']
for filename in filenames:
method(filename)