0

I am trying to combine several excel workbooks using jupyter notebook. I named the directory containing the excel workbooks files and its output is:

['Book10.xls',
 'Book13.xls',
 'Book12.xls',
 'Book16.xls',
 'Book9.xls',
 'Book15.xls',
 'Book14.xls',
 'Book8.xls',
 'Book18.xls',
 'Book4.xls',
 'Book6.xls',
 'Book3.xls',
 'Book1.xls']

I'm using the following loop to try to extract the data into python and paste them together in a larger dataframe:

df = pd.DataFrame()
for file in files:
     if file.endswith('.xls'):
         df = df.append(pd.read_excel(file), ignore_index=True) 
df.head()

However, I'm the getting a path error:

[Errno 2] No such file or directory: 'Book10.xls'

I read a similar thread (Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory) which mentioned that using an IDE can change the file path. I also tried os.listdr() and os.getcwd to double check I'm in the correct directory (which I seem to be).

Any help on figuring out why the file path isn't being recognized and suggestions to get the loop working would be greatly appreciated!

codextrmz
  • 27
  • 7
  • Wht is the directory you are working in i.e. the jupyter notebook? and where the files are saved? – Anurag Dhadse Jun 04 '21 at 04:47
  • Add tags `pandas` `python` also as it mainly involves the two. – Anurag Dhadse Jun 04 '21 at 04:48
  • The directory I am working in is saved under `'/Users/Name/Project'` while the files are saved in `/Users/Name/Downloads/Data Name/Data Folder1/Data Folder2/Data Folder3`. I already see the problem, do I need match the file path name to the directory where jupyter notebook lives? – codextrmz Jun 04 '21 at 13:23
  • 1
    If you are not providing the absolute path to the folder containing the files, then it's better to keep the Jupyter Notebook and the files in the same directory. –  Jun 04 '21 at 13:27
  • What @SanskarSingh said makes sense. try keeping them in the same working directory to simplify. Otherwise, use absolute paths. – Anurag Dhadse Jun 04 '21 at 14:24
  • I moved the folder to the working directory and it has a pathname of `/Users/Name/Project/Folder1`. The working directory (jupyter) still has the same pathname `/Users/Name/Project`. I would think that they would be in the same directory but I am still getting the same error (Errno2). Any suggestions? – codextrmz Jun 04 '21 at 15:40

0 Answers0