FileNotFoundError when I call a file with read_csv from an other directory

73 Views Asked by At

In my code I have a file called location.py where I read a file that I use for a function assign:


import pandas as pd

filename = '../../Dataset/Import.csv'
df = pd.read_csv(filename, sep='\t', encoding='utf-16')


def assign(df = df):

Now, I would like to use the function assign in another file, that is in an other directory, but I meet the error:

FileNotFoundError: [Errno 2] No such file or directory:

If I go back in location.py and I change the relative path of the filename, everything works. But I was expecting python to read the relative path of the folder where it is written, not from where I call the function. This is the directory structure

  • Directory a -- location.py
  • Dataset -- Import
  • Directory b -- another directory -- file_from_where_I_call_location.py

I still would like to use a relative path, but how can I solve this issue? I can't keep changing the file path hard coded every time I need it

1

There are 1 best solutions below

0
On

That is because the relative path "../../Dataset/Import.csv" is resolved relative to the current work directory. You can retrieve the script's directory by using pathlib.Path and __file__ with pathlib.Path(__file__).parent:

import pandas as pd

from pathlib import Path

script_path = Path(__file__).parent

file_path = script_path / "../../Dataset/Import.csv"

# or if you want an absolute path (however, not needed for pd.read_csv)
# file_path = (script_path / "../../Dataset/Import.csv").resolve()

df = pd.read_csv(file_path, sep="\t", encoding="utf-16")

def assign(df):
    pass  # TODO something