32  Exercise: Handling errors and files

We’ve now covered exception handling, as well as reading and writing .csv files (without Pandas - in reality, you’ll probably use Pandas’ in-built method more often for the kind of work you’ll do - more on that later).

Now work through the following notebook.

Open Exercise in Google Colab: Open In Colab

To access the file input_data.csv in colab, you will need to upload it to Google Colab.

First, download it by right clicking on the following link and choosing ‘save file as’: Download Link

Once in Colab, 1. Click on ‘files’ in the left hand bar in Google colab (the folder-shaped icon) 2. Choose ‘upload to session storage’ 3. Select the file ‘input_data.csv’ from your machine. You can download it if required - right click on this link and choose ‘save file as’ 4. Click ‘ok’ if a warning message appears. input_data.csv should now appear in your list of files. Hover over it and three dots will appear - click on these. 5. Choose ‘copy path’ to get the filepath of the csv file. You can then right click and choose ‘paste’ in a code cell to get the filepath you will need in your code.

32.1 Sample Answers

Open exercise solutions in Google Colab: Open In Colab

You will need to upload the dataset to the solution notebook if you want to run the solutions.

32.2 Answer Video

32.3 Task 1

32.4 Task 2

**There were two things I forgot to explain in this solution video!

  • The file input_data.csv is in the same folder as the notebook we are working on, so we can just set our file path to be “input_data.csv” rather than having to give the full filepath (like “C:/Users/Sammi/HSMA Exercises/HSMA 1F/Datasets/input_data.csv”)

  • in the line with open("input_data.csv", "r") as my_file:, the “r” refers to the access mode of the file. “r” just means to want to read the file, as opposed to write new data to it (which would use w).