'Opening a csv file with pandas with relative path from another main file in another path

My question is similar to the one in link but with a slight difference

the project structure is

project_folder
--> main.py
--> scripts_folder
    --> script.py
--> data_folder
    --> data.csv

The main.py looks like this

from scripts_folder.script import a

a()

The script.py looks like this

import pandas as pd

df = pd.read_csv('../data_folder/data.csv')

# do some things with df


def a():
    print('something')

I am running main.py, which calls script.py, which has to import data from data.csv. I tried the method given in the question above. ie I tried importing using this line of code in scripty.py pd.reac_csv('../data_folder/data.csv') but it is not working.

It is giving me error

    handle = open(
FileNotFoundError: [Errno 2] No such file or directory: '../data_folder/data.csv'

[EDIT]

I tried moving the data.csv into scripts_folder as in

project_folder
--> main.py
--> scripts_folder
    --> script.py
    --> data.csv
--> data_folder (empty)

and I changed it to df = pd.read_csv('data.csv') and still I am getting the same error

    handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'

Can someone help? Thanks



Solution 1:[1]

 handle = open(
FileNotFoundError: [Errno 2] No such file or directory: '../data_folder/data.csv'

This indicates that it supposed to work inside scripts_folder. ../data_folder means get the directory up relative to the current and find data_folder in this directory.

You could construct absolute file path for csv-file using os.path.abspath and os.path.join and give it to script reading data. To debug current working directory for script, use os.getcwd()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aestet