1

I have python code in Jupyter notebook and accompanying data in the same folder. I will be bundling both the code and data into a zip file and submitting for evaluation. I am trying to read the data inside the Notebook using pandas.read_csv using a relative path and thats not working. the API doesnt seem to work with relative path. What is the correct way to handle this?

Update:
My findings so far seem to suggest that, I should be using os.chdir() to set the current working directory. But I wouldn't know where the zip file will get extracted. The code is supposed to be read-only..So I cannot expect the receiver to update the path as appropriate.

Aadith Ramia
  • 10,005
  • 19
  • 67
  • 86
  • 1
    It may not be possible in general, see here: https://stackoverflow.com/questions/52119454/how-to-obtain-jupyter-notebooks-path – John Zwinck Sep 12 '20 at 07:19

2 Answers2

3

You could append the current working directory with the relative path to avoid problem as such:

import os
import pandas as pd

BASE_DIR = os.getcwd()
csv_path = "csvname.csv"

df = pd.read_csv(os.path.join(BASE_DIR, csv_path)

where csv_path is the relative path.

Hatim
  • 417
  • 6
  • 8
-1

I think first of all you should make a unzip file then you can run.

You may use the below code to unzip file,

from zipfile import ZipFile
file_name = "folder_name.zip"
 
with ZipFile(file_name, 'r') as zip:
  zip.extractall()
  print("Done !")
Imdadul Haque
  • 1,635
  • 5
  • 23
  • 44