BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Lab 4d — Files


1 minute

Download the zip file for this lab, located above. This zip file has code that you will use for this assignment. Extract the files and put them in your cs110 directory in a folder called lab4d.


These problems will help you practice the file processing pattern discussed this week:

get the arguments, call main, then do the file processing pattern

  • get the arguments
  • call the main function and pass it the arguments
    • read a file into a list of lines
    • change the lines in some way
    • write the new list of lines to a file

You can see the guide on reading and writing files for additional help.


15 minutes

This program is designed to help you safeguard sensitive information in a file. The program reads a file, replaces all digits with the # symbol, and then writes the results to a new file.

We have provided most of the code in This code implements the entire pattern shown above except the middle step — “change the lines in some way”. You will need to implement this piece to replace all the digits with the # symbol.

We have given you a file, receipts.txt, which starts with this:

Purchase by: Gordon Bean
Item: 50 cases of diet coke
Amount: $500
Card: 2355-4456-8301-2213

If you run the program with:

% python receipts.txt safe.txt

then the file safe.txt should contain:

Purchase by: Gordon Bean
Item: ## cases of diet coke
Amount: $###
Card: ####-####-####-####

There are additional lines in receipts.txt that have sensitive data.

Discuss with the TA:

  • How do you replace all of the digits in a single line with a #?
  • How do you do this for all of the lines?
  • What list pattern is this?

Double space

10 minutes

For this problem, you will write an entire program from scratch. It follows the same file processing pattern, so we encourage you to copy the relevant parts from the prior program.

This program should:

  • read in a file
  • add a blank line after every line
  • write the results to a new file

The program takes the input and output files as arguments. We have given you a file called still-i-rise.txt, which contains a famous poem by Maya Angelou, a civil rights activist. When you run the program:

% python still-i-rise.txt doubled.txt

then the file doubled.txt should contain the same poem, but double spaced.

Discuss with the TA:

  • Which parts of the code from the previous problem do you need to copy?
  • Do you understand what this copied code is doing?
  • How do you add a blank line after every line? (There are multiple good ways to do this.)
  • What list pattern is this?
  • What questions do you have about reading and writing files or the file processing pattern?


10 minutes

Python programs cannot be debugged if run through the terminal, so the TA will show how to add program arguments to a run configuration in Pycharm. By right-clicking a play button next to if __name__ == '__main__':, you can click Modify Run Configuration to add parameters. It can be helpful to right-click the parameters box to add a path.


Discuss with the TA:

  • Where do you convert the filename into the contents of the file?
  • What type of object does readlines return?
  • How do you view that object when debugging?
  • Where do you iterate through the contents of the file?
  • How do you view each line when debugging?
  • How do you view each line after it has been modified?
  • What type of object does writelines accept?


To finish this lab and receive a grade, take the canvas quiz.


We are providing a solution so you can check your work. Please look at this after you complete the assignment. 😊