Lesson 05: Working with files, variable scope, error handling, and iterable unpacking

This week you will learn how to create, read, and modify files stored in the file system. Data previously embedded in the *.py file will instead be stored in files that you will access programmatically. You will work with plain text files and comma separated value files known by the acronym CSV.

You will also learn how to utilize the Python Standard Library’s pathlib module when working with file paths and the csv module when interacting with CSV files.

You will also encounter functions that feature a docstring, a multiline string that is used to document a function’s behavior, parameters, and return value (if any).

It’s also time to learn about the concept of variable scope and how it impacts the visibility of an object within a program.

Over the last few weeks you’ve likely triggered a runtime exception that has terminated execution of your code prematurely. Exceptions are reported in the form of a “traceback” that is streamed to the terminal. Exceptions commonly encountered include AttributeError, FileNotFoundError, IndexError, NameError, SyntaxError, TypeError, and ValueError, Besides acquiring a better understanding of these sorts of errors we will also discuss scenarios when it makes sense to “catch” exceptions using a try/except block in order to respond programmatically to errors and avoid the premature termination of your program.

Finally, You will learn how to unpack iterables such as lists and tuples, a feature that allows you to assign the elements of a sequence to multiple variables in a single statement.

Readings

Below are a set of readings that you should consider reviewing. Note that the Real Python articles referenced below may require signing up for a free account.

1.0 Required

Eric Matthes, Python Crash Course, 3rd Edition (No Starch Press, 2023).

Chapter 10 Files and Exceptions:

Read the following sections:

  • Reading from a File
  • Writing to a File

Lisa Tagliaferri, How To Import Modules in Python 3 (Digital Ocean, Sept 2016).

Lisa’s article will introduce you to Python’s modular and extensible architecture and how to use the import statement in your code in order to access Python modules and their functions,classes, and variables. Module imports is all about code reuse. This week you will import the csv module in order to simplify working with CSV files. Later you will import and use other modules such as datetime, json, and requests.

Below are a set of useful readings that you should consider reviewing. Note that the Real Python articles referenced below may require signing up for a free account.

Naomi Ceder, The Quick Python Book, 3rd Edition (Manning Publications, 2018).

Read Chapter 12, Using the filesystem, sections:

  • 12.1 os and os.path vs pathlib
  • 12.2 paths and pathnames (subsections 12.2.1 - 12.2.5)

Jon Fincher, Reading and Writing CSV Files in Python (Real Python, nd).

Jon discusses how to read from and write to comma-separated values files (CSV) using the Python Standard Library’s csv module. Read the first two sections:

  • What is a CSV file?
  • Parsing CSV Files With Python’s Built-in CSV Library

Chad Hansen, Understanding the Python Traceback (RealPython, 2019).

We’ve been dipping in and out of Chad’s useful article. Never hurts to review it again.

Trey Hunner, Why you should be using pathlib, (treyhunner.com, Dec. 2018).

Trey discusses and promotes adoption of the pathlib module, an alternative to and newer module for representing file system paths.

Trey Hunner, No really, pathlib is great, (treyhunner.com, Jan. 2019).

A useful followup to Trey’s Dec. 2018 post in which he responds to criticims of his Dec 2018 blog post.

Geir Arne Hjelle Python 3's pathlib Module: Taming the File System (Real Python, April 2018).

Geir outlines the case for switching to pathlib.Path.

Said van de Klundert, Python Exceptions: An Introduction (Real Python, nd).

Focus in particular on the sections that discuss the try and except block.

James Mertz, Documenting Python Code: A Complete Guide (Real Python, nd).

James makes the case for documentating the code you write, moving beyond the occasional comment to using Docstrings to more fully describe how your program and its objects are expected to behave.

Michelle Morales, How to Handle Plain Text Files in Python 3 (Digital Ocean, Oct. 2016).

A useful primer (and a quick read).

Leodanis Pozo Ramos, LBYL vs EAFP: Preventing or Handling Errors in Python (Real Python, June 2022).

Solid overview of two error handling strategies: “Look before you leap” (LBYL) and “Easier to ask for forgiveness than permission” (EAFP).

Charles Severance, Python for Everybody. Exploring data in Python 3 (CreateSpace, 2016).

💡 An English-language version of Chuck’s book is available as a PDF or ePub in the Canvas Files texts folder. Other translations and formats are available online.

Read Chapter 7 “Files”. Chuck discusses basic file operations (creating, opening, reading from, writing to, appending) as well as try/except blocks.

John Sturtz, Defining Your Own Python Function (Real Python, March 2020).

John’s article is a long read but worth the time investment. His discussion of the role functions play in enhancing code modularity is solid; so too is his detailed overview of the various flavors of function argument passing.

3.0 Reference

w3schools, Python File methods (w3schools.com, nd).

Not quite a cheat sheet but close. Use it as a reference.

Eric Matthes, Beginner’s Python Cheat Sheet - Files and Exceptions.