I am trying to read all files in a directory and then combine each of those csv files to a single excel sheet. Here is my code so far
import pandas as pd
import datetime as dt
import os
import glob
#set Path to Test Directory
os.getcwd()
mwd = os.chdir('Test')
#Create a list with all CSV Files
files = os.listdir(mwd)
#Create a blank dataframe
combined = pd.DataFrame()
for file in files:
df=pd.read_csv(files)
combined = combined.append(df,ignore_index = TRUE)
combined.to_excel('testing.xlsx' , index = False)
When running the code , I get the following error
File "C:\Users\x\Documents\automation\Testing.py", line 19, in <module>
df=pd.read_csv(files)
File "C:\Users\x\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "C:\Users\x\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\Users\x\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 575, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\x\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 934, in __init__
self._engine = self._make_engine(f, self.engine)
File "C:\Users\x\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1233, in _make_engine
raise ValueError(msg)
ValueError: Invalid file path or buffer object type: <class 'list'>