I am trying to merge multiple workbooks into one. However, I am currently having issues regarding my code.
cwd = os.path.abspath(r'C:\xxx\xxx\Documents\xxx\xx Data')
files = os.listdir(cwd)
print (files)
df = pd.DataFrame()
for file in files:
if file.endswith('.csv'):
df = df.append(pd.read_excel(file), ignore_index=True)
df.head()
This code returns an error on line 7.
FileNotFoundError: [Errno 2] No such file or directory: 'Code.csv'
Can anybody help me as I am not sure how/why cant it find the file.
CodePudding user response:
The screenshot of your folder doesn't seem to have a .py file. I'm assuming it's in another folder.
Try
os.chdir(cwd)
before the for loop.
EDIT - Alternatively, if you do not want to change your working directory, you can specify the path to the file.
pd.read_csv(cwd "/" file)