O'Reilly logo
  • Longxiang Lyu thinks this is interesting:

10-8. Cats and Dogs:

From

Cover of Python Crash Course

Note

import os filenames = {'cats.txt', 'dogs.txt', 'notexist.txt'}

def print_file(filename): print("\n* " + filename + " ***") try: with open(filename, 'r') as f_o: content = f_o.read() except EnvironmentError as e: print(filename + ': ' + os.strerror(e.errno)) else: print(content)

for name in filenames: print_file(name)