Anthony Mukanya
Anthony Mukanya

Reputation: 1

Why colab notebook does not recognise a file in a set working directory?

I tried running a python script using the following methods:

Basically, I uploaded all the .nc files to my google drive plus an excel file with some data that are needed in the code. The code stops at some point and returns an error message saying that there is no such file although clearly the files is there.

Here is the code:

import os
from netCDF4 import Dataset
import numpy as np
import pandas as pd

os.chdir('/content/drive/MyDrive/Precipitations') # to set a new working directory

date_range=pd.date_range(start="19960101", end="20200930")
df = pd.read_excel('/content/drive/MyDrive/Precipitations/stationssa.xlsx')df1=pd.DataFrame(0,columns=df.loc[:,"NAME"],index=date_range)

for position in stations:
    lat_station = df.iloc[position,1]
    lon_station = df.iloc[position,2]
    for day in date_range:
        data = Dataset("gpcp_v01r03_daily_d"+str(day)[0:4]+str(day)[5:7]+str(day)[8:10]+".nc")
        prcp=data.variables['precip'][:]
        lon_data = data.variables['longitude'][:]
        lat_data = data.variables['latitude'][:]
        sq_diff_lat = (lat_data - lat_station)**2
        sq_diff_lon = (lon_data - lon_station)**2
        min_index_lat = sq_diff_lat.argmin()
        min_index_lon = sq_diff_lon.argmin()
        if type(prcp[0, min_index_lat, min_index_lon])==np.ma.core.MaskedConstant:
            df1.loc[str(day),position] = -1
        else:
            df1.loc[str(day),position] = float(prcp[0, min_index_lat, min_index_lon])
df1=df1.replace(-1,np.nan) ### when the dataframe is done
df1.to_csv("Preciptations.csv") ## to export to a csv file 

The link to the colab notebook is here: <https://colab.research.google.com/drive/1q5e1nuVHfPJsKba2ImoBv456aoO9gu0s?usp=sharing

But I get this error message:

enter image description here

Upvotes: 0

Views: 714

Answers (1)

Mohana
Mohana

Reputation: 489

You need to mount your drive first, using

from google.colab import drive
drive.mount('/content/drive')

Upvotes: 1

Related Questions