Python Forum

Full Version: Merging Excel Files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
We are new to Python script and need to merge 50+ excel files into one workbook. Have searched for suitable code and have edited to suit our data & folder structure but have now got same 'file not found' error on 2 different PCs. Code is below! Any advice welcome! Thanks J&M

import os
import pandas as pd
cwd = os.path.abspath(' ') 
files = os.listdir(cwd)
df = pd.DataFrame()
for file in files:
    if file.endswith('.xlsx'):
        df = df.append(pd.read_excel(file), ignore_index=True) 
df.head() 
df.to_excel('Merge.xlsx')
Error
FileNotFoundError: [Errno 2] No such file or directory: 'Test_Excel_1.xlsx'
On line 3, you've asked it to turn the relative path of " " into an absolute path. Do you have a directory that is just a space? That seems unusual.

I would expect the os.listdir on line 4 to error out with no such directory. But your error seems to be later (please give the entire traceback, not just one line).