Saravji's Hut

Logo

The place where change happens.

Project on GitHub Saravji/saravjis_hut

Saravji's Hut YouTube channel

Saravji on Twitter

Foundations of Programming (Python)

LAB 07-B: Working with Binary Files

In this Lab, we’ll continue with our SimpleMath program:
You’ll change the file input / output functions to work with binary files.
Additionally, you’ll add the functionality to select the operation mode via program arguments (see Module 03): Either processing the calculation or displaying the results and user input for the numbers used in the next calculation.

  1. Make a copy of the LAB07_A.py file and save it as LAB07_B.py.
  2. Add code to read in the program arguments and create distinct operational paths: As program arguments we’ll use ‘calc’ and ‘IO’.
  3. Change the functions for the file access to use binary files. Change the names of the files to numbers.dat for the numbers file and results.dat for the results file.
  4. For the ‘IO’ path, add code to display the current content of the results file. Ask the user for input two numbers and save these to file.
  5. For the ‘calc’ path, read in the numbers from files, do the calculations and write out the results into file.
  6. Test the script and write down how the code works.
  7. Think about what this is useful for?

Back to Modules Materials Lists