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
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 usingpathlib.Path
and__file__
withpathlib.Path(__file__).parent
: