Week 9: Files

Warning

This page shows last year’s version. We might still make small changes, but you’re welcome to take a look. We’ll remove this notice once the page is final.

Week 9: Closure#

Syllabus#

  • The module os an interface to interact with the operating system

  • Current working directory os.getcwd()

  • Directory list os.listdir()

  • Check if the file exists os.path.isfile() , also os.path.isdir()

  • Relative and absolute paths, os.path.join()

  • The function open() with 'r' and 'w'

  • The statement with and alias as for simplified handling of exceptions

  • Reading methods: read() , readlines()

  • Splitting lines with splitlines()

  • Writing methods: write() , writelines()

  • More on string escape sequences, repr() and strip()

  • Reading csv files as text files

Checkpoints#

Checkpoint 9.1: Nitrate Levels #

Once a week, samples of drinking water are tested for nitrate. The test results are stored in a file where each line contains a floating-point number representing one nitrate level measurement. Nitrate levels are categorized as:

  • Very low: Nitrate levels less than or equal to 4.0 mg/l.

  • Low: Nitrate levels above 4.0 but less than or equal to 9.0 mg/l.

  • Normal: Nitrate levels above 9.0 and below 40.0 mg/l.

  • High: Nitrate levels greater than or equal to 40.0 but below 50.0 mg/l.

  • Very high: Nitrate levels greater than or equal to 50.0 mg/l.

Note here that when the nitrate level falls on the border between two categories, it is included in the category further from normal. For example, a nitrate level of 4.0 mg/l is very low, and a nitrate level of 40.0 mg/l is high.

Write a function that takes a string containing the file name with the nitrate levels as input. The function should return the number of weeks where the nitrate levels were very low, low, normal, high, and very high, respectively, as shown in the example below.

Consider the file week_09_files/nitrate_data_A.txt with the content below.

34.5
34.9
36.7
29.9
34.5
44.5
34.5
46.5
29.9
34.5

None of the values are below 9.0, so none belong to the lower two categories. Eight values are in the range from 9.0 to 40.0, classifying them as normal. Two values are between 40.0 and 50.0, placing them in the high category. There are no values that are classified as very high. The function therefore returns 0, 0, 8, 2, 0.

The expected output may be seen in the example.

>>> nitrate_levels('week_09_files/nitrate_data_A.txt')
(0, 0, 8, 2, 0)

The filename and requirements are in the box below:

nitrate_levels.py

nitrate_levels(filename)

Return the number of weekly measurements in each category.

Parameters:

  • filename

str

Filename of the data file.

Returns:

  • tuple

Number of measurements in each of five categories for nitrate levels.

Use the following script to check your function test_nitrate_levels.py. If your function fails the test in this script, it will also fail when you hand it in.

Checkpoint 9.2: Count Differences #

The results of an experiment are recorded by two independent observers. The observers record the results as a sequence of comma-separated integers, which is saved in a file containing one line of text. We need to count the number of differences between the recorded results of the two observers.

Write a function that takes as input two strings containing the names of the files with the experiment results. If the number of results in one file is different from the number of results in the second file, the function should return -1. If the number of results is the same in the two files, the function should return the number of results that the two observers have recorded differently. Consequently, the function should return 0 if the results in both files are the same.

As an example, consider the two files below.

>>> filename1 = 'week_09_files/results_A1.txt'
>>> filename2 = 'week_09_files/results_A2.txt'

The content of the first file is:

345, 349, 367, 299, 345, 445, 345, 465, 299, 345

The content of the second file is:

345, 349, 367, 300, 354, 445, 345, 465, 300, 345

Both files contain 10 recorded results, so we inspect each pair of recorded results. The first three pairs are the same (345, 349, 367) but the fourth pair is different (299 and 300). Furthermore, the fifth and ninth pairs are different. The function should therefore return 3, as shown in the code cell below.

>>> count_differences(filename1, filename2)
3

The filename and requirements are in the box below:

count_differences.py

count_differences(filename1, filename2)

Number of differences in recorded results.

Parameters:

  • filename1

str

Filename of the first file.

  • filename2

str

Filename of the second file.

Returns:

  • int

Number of differences in recorded results.

Use the following script to check your function test_count_differences.py. If your function fails the test in this script, it will also fail when you hand it in.