Search code examples
pythonpathabsolute-pathos.pathpathlib

python; reading file path error


i have a directory structure;

DIR1: ----outerPyFile.py ----DIR2: --------innerPyFile.py --------DIR3: ------------fileToRead.csv


I'm reading fileToRead.csv in innerPyFile: pd.read_csv('DIR3/fileToRead.csv') works fine if i run innerPyFile.py individually

Now when import innerPyFile module inside outerPyFile.py as
import innerPyFile
-- FileNotFoundError: DIR3\\fileToRead.csv. does not exist

i tried replacing path with absolute path in innerPyFile as pd.read_csv(os.path.abspath('DIR3/fileToRead.csv'))

still, when i run outerPyFile i get,
FileNotFoundError C:\\\DIR1\\\DIR3\\\fileToRead.csv does not exist,

here the code omitted DIR2 so i changed code as pd.read_csv(os.path.abspath('DIR2/DIR3/fileToRead.csv'))

Now the code structure works file when i run outerPyFile.py which is acceptable. but here the problem will arise when i run innerPyFile individually because it will search for DIR2 which is not there in CWD of innerPyFile.

anyone can suspect this behavior,
please revert me what is going on?

FYI, I've also tried pathLib module which didn't solve the issue.


Solution

  • Try this:

    innerPyFile.py

    import os
    script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
    script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
    rel_path = "DIR3/fileToRead.csv"
    abs_file_path = os.path.join(script_dir, rel_path)
    
    pd.read_csv(abs_file_path)
    

    outerPyFile.py

    import DIR2.innerPyFile
    #......do something.....