Aadith Ramia
Aadith Ramia

Reputation: 10329

pandas : read_csv not accepting relative path

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.

Upvotes: 1

Views: 2555

Answers (2)

Hatim
Hatim

Reputation: 447

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.

Upvotes: 3

Imdadul Haque
Imdadul Haque

Reputation: 1855

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 !")

Upvotes: -1

Related Questions