I want to read an excel file into pandas DataFrame. The module from which I want to read the file is inputs.py and the excel file (schoolsData.xlsx) that I want to read is outside the folder containing the module. I’m doing it like this in my code
def read_data_excel(path): df_file = pd.read_excel(path) return df_file school_data = read_data_excel('../schoolsData.xlsx')
Error: No such file or directory: ‘../schoolsData.xlsx’
The strange thing is that it works fine when I run the function containing this code locally but I get an error when I run the function after installing my published package from PyPi.
What is the right way to do it? Also would is it possible to read the file normally from the installed distributable that is a compressed folder?
Advertisement
Answer
Your code should work. Try this to check the folder you are at:
import os os.path.dirname(os.path.realpath(__file__))