Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Merging Excel Files
#1
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'
Reply
#2
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).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 445 Feb-07-2024, 12:24 PM
Last Post: Viento
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,179 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  How to loop through all excel files and sheets in folder jadelola 1 4,506 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Creating csv files from Excel file azizrasul 40 5,709 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  Help needed with merging two CSV files eyadfr 5 2,981 Dec-14-2021, 07:34 PM
Last Post: paul18fr
  Working with excel files arsouzaesilva 6 3,186 Sep-17-2021, 06:52 PM
Last Post: arsouzaesilva
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,904 Aug-20-2021, 05:42 PM
Last Post: snippsat
  win32com — How to resolve “AttributeError: xlUp” for Excel files? JaneTan 2 4,244 Aug-18-2021, 05:27 AM
Last Post: snippsat
  Merging all file_name.log's files from directory to one and search “PerformanceINFO" sutra 0 1,792 Dec-09-2020, 05:14 PM
Last Post: sutra
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,864 Dec-06-2020, 04:14 PM
Last Post: Divya577

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020