9 Files and Exceptions

Objectives

In this chapter you’ll:

  • Understand the notions of files and persistent data.

  • Read, write and update files.

  • Read and write CSV files, a common format for machine-learning datasets.

  • Serialize objects into the JSON data-interchange format—commonly used to transmit over the Internet—and deserialize JSON into objects.

  • Use the with statement to ensure that resources are properly released, avoiding “resource leaks.”

  • Use the try statement to delimit code in which exceptions may occur and handle those exceptions with associated except clauses.

  • Use the try statement’s else clause to execute code when no exceptions occur in the try suite.

  • Use the try statement’s finally clause to execute code regardless of whether an ...

Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.