Finding the File Path for Working on Jupyter Notebook Locally
The module leader has provided a starter Python code to help you merge the multiple crime
data spreadsheets from the Metropolitan Police crime statistics website into one
DataFrame.
If you try to run this code as is, you will most likely run into an error.
This error will be caused by these specific lines of code:
This is because these lines of code use what we call an absolute file path. An absolute file
path is a type of “address” (similar to a URL) that points to a specific file or folder on a
particular device, which is in this case, a file and a folder on the module leader’s computer,
which seems to be a Linux machine.
You have to update these paths to point to the file and folder located on your computer.
Step 1: Extract the Crime Statistics Zip File and Find Your Main “data” Folder’s Path
- First, make sure you have downloaded the crime statistics .zip file from the
[Link] website. When you extract (or unzip) this file, you will get a collection
of folders, one for each month of data (e.g., 2023-08, 2023-09, etc.). Take note of
these as we will need them in the next step.
- Navigate to your project folder (where your .ipynb Jupyter Notebook file is located, Ex.
“C:/Users/YourName/Documents/CST1440/Assessment1”) and create a folder
called “data”. Within the “data” folder, create another folder called “police” then copy
the collection of folders from the previous step into the “police” folder.
- We will now then get the path needed for the Python code. Navigate back to your
project folder that contains the “data” folder.
On Windows
1. While in your project folder, right click on the “data” folder then click “Copy as Path”.
Alternatively, you can also copy the path on the address bar at the top of the File
Explorer window. Just make sure to add the “data” folder at the end.
2. You should now have a path in your clipboard similar to:
“C:\Users\YourName\Documents\CST1440\Assessment1\data”
Replace the path in the [Link] section of the code with this.
3. Do the same with the first/oldest dataset, [Link] to get a
pathname similar to:
“C:\Users\YourName\Documents\CST1440\Assessment1\data\police\2023-
08\[Link]”
Replace the path in the pd.read_csv section at the beginning of the code with this.
4. IMPORTANT: You cannot paste the addresses directly on the code block as
Windows and Python have different syntaxes for file paths. You first need to do one
of the following options before running the code cell:
a. Add an “r” at the beginning of the path, before the opening quotation mark.
This prevents Python from trying to read characters after backslashes as
escape codes.
b. Replace all backslashes in the path to forward slashes.
On macOS
1. While in your project folder, right click on the “data” folder. Hold the “Option ⌥” key,
“Copy” will change to ‘Copy “data” as Pathname’ while the key is held, then click it.
2. You should now have a path in your clipboard similar to:
“/Users/YourName/Documents/CST1440/Assessment1/data”.
Replace the path in the [Link] section of the code with this.
3. Do the same with the first/oldest dataset, [Link], to get a
pathname similar to the following:
“/Users/YourName/Documents/CST1440/Assessment1/data/police/2023-08/2023-
[Link]”
Replace the path in the pd.read_csv section at the beginning of the code with this.