A document from MCS 260 Fall 2021, instructor Emily Dumas. You can also get the notebook file.

MCS 260 Fall 2021 Homework 8

  • Course instructor: Emily Dumas


  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)


This homework assignment must be submitted in Gradescope by 10am CST on Tuesday, October 19, 2021.


This homework focuses on exceptions, the os module, and the function- and assignment-related concepts from Lecture 20 (multiple return values, tuples, tuple assignment, variadic functions, iterable unpacking).


Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

The course materials you may refer to for this homework are:

Point distribution

This homework assignment has 2 problems, numbered 2 and 3. The grading breakdown is:

Points Item
2 Autograder
4 Problem 2
4 Problem 3
10 Total

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

( 1. There's no problem 1 )

Gradescope will show the results of the automated syntax check of all submitted files as the score for problem 1.

2. Robust chemical element CSV to JSON

In Homework 7, problem 3 (titled "Chemical element CSV to JSON"), you wrote a program to convert a CSV file containing data about chemical elements to a JSON file. The program used hard-coded filenames elements.csv and elements.json.

Modify this program (either start with one of the solutions posted to the course web site, or your own solution) so that it is robust to all of the following conditions:

  • elements.csv does not exist
  • elements.csv exists and is a CSV file, but it has fewer than 3 columns
  • elements.csv exists and is a CSV file with columns called number, abbreviation, and name, but some of the values in the column number are not integers

If any of these conditions arises, the program should print an error message describing the situation and then exit without creating elements.json nor writing anything to it.

Hint: You can detect these error conditions because they cause exceptions. I recommend forcing each of these things to happen and checking the type of exception that results.

Save the result as hwk8prob2.py.

3. Listing Python files containing the word "Lecture"


Most of the sample programs we've written in lecture contain a comment listing the lecture or lectures in which we worked on or discussed the program. Usually such a comment looks like this:

# Lecture 12

or this:

# MCS 260 Fall 2021 Lecture 9

This problem is motivated by the question: How could you write a Python script to find all such example programs that mention a specific lecture?

Your task

Write a program that prints a list of names of all files in the current directory that meet both of the following criteria:

  1. The filename ends with .py.
  2. The text inside the file contains the string Lecture somewhere.

To test this program, you should probably create another file in the current working directory that ends with .py and which contains the string Lecture somewhere!

You don't need to split a file into words for this problem; for example, if a file has first line asdfLecturefghil and its name ends in .py, this program should print its name.

Save the result as hwk8prob3.py.

Restricted methods note: For full credit your answer may only import these modules: os, json, and csv.

BE CAREFUL: This program should only open files for reading. It would be dangerous to open all files in the current directory for writing, because that would delete the current contents!

Example: When I run a program meeting the specifications of this problem with the current working directory set to the samplecode directory for MCS 260, the output is:


Revision history

  • 2021-10-14 Initial release
  • 2021-10-14 Fix typo in problem 2