0

I am reading a python subscript into a python script. The subscript uses an excel file in it's working directory. The structure looks like this

main_folder ->
  main.py
  subfolder ->
   data.xlsx
   submain.py

My main script calls the subscript as such:

from subfolder.submain import df

My submain.py script looks as such:

df = pd.read_excel('data.xlsx')

However on my main.py script I get the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'data.xlsx'

This is strange because not only does submain.py run fine by itself, a quick os.listdir() shows the file to be there:

for f in os.listdir('subfolder'):
print(f)

data.xlsx
submain.py

Does anyone understand this behaviour? Many thanks

geds133
  • 1,503
  • 5
  • 20
  • 52
  • main.py works in its containing folder, that does not contain data.xslx. Now I'm not sure what's the best way to fix that is. – Swifty Dec 16 '22 at 10:55
  • Finally found a post that addresses this: https://stackoverflow.com/questions/10174211/how-to-make-an-always-relative-to-current-module-file-path – Swifty Dec 16 '22 at 11:25

1 Answers1

0

It's a path problem: since submain.py is imported by main.py, your file is looked for in the working directory, main_folder; to fix that, add the absolute path to your pd.read_excel statement in submain.py:

import os
df = pd.read_excel(os.path.join(os.path.dirname(__file__), 'data.xlsx'))
Swifty
  • 2,630
  • 2
  • 3
  • 21