Home > Mobile >  Python - using open() function [closed]
Python - using open() function [closed]

Time:09-24

I'm kind of new to Python & still lack some understanding of codes. I'm trying to open an excel file and save it as a DF (dataframe) using Pandas.

with open('dobTryOut.xlsx','rb') as df:
   df = pd.read_excel('dobTryOut.xlsx')
   print(df)

However, it is somehow not opening in my own Jupyter notebook. Any ideas?

I've also tried using my system's directory but it still gives the same error, saying: FileNotFoundError: [Errno 2] No such file or directory: 'dobTryOut.xlsx'

CodePudding user response:

You could just write it like this

df = pd.read_excel(file_name)

there is no need of open function here

CodePudding user response:

This is a problem with the relative path. When you do this:

open("myfile")

You are really doing this:

open("./myfile")

That is, open myfile in the current directory. Since your spreadsheet is not in the current directory, it's failing. You can either use the absolute path C:\whatever or /home/whatever, or you can use a correct relative path.

Note that as per the other example there's a helper function in pandas, but this won't help with that error until you get the path right.

And please don't post images or (worse) links to images for code and errors...

  • Related