Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to combine all files in a directory and the save the combined file into another directory. I am using Python 3.8. When I run the code I get the following with a AttributeError:

c:\test\Upload test\Book1.xlsx
c:\test\Upload test\Book2.xlsx
c:\test\Upload test\Book3.xlsx
Traceback (most recent call last):
File "C:/Python/PythonDev/combine.py", line 104, in <module>
newdf.to_excel(writer,"All")
AttributeError: 'NoneType' object has no attribute 'to_excel'

the code:

import pandas as pd
import globe
filelist = glob.glob(r'c:\test\Upload test\*.xlsx')
file1 = "*.*"
for i in  filelist:
    file2 = pd.read_excel(i)
    file2['FileName'] = i
    file1 = ['newdf']
    newdf = file1.append(file2)
    print (i)
writer = pd.ExcelWriter(r'c:\test\Uploaded\uploadfile.xlsx', engine= 'xlsxwriter')
newdf.to_excel(writer,"All")
writer.save()
                sorry for the typo. question should read  "I am trying to combine all files in a directory and then save the combined file into another directory."
– user8763797
                May 7, 2020 at 16:52
                Thanks, that has removed the error, but the saved file (uploadfile.xlsx) is empty. So the code is reading the files and generating an empty file, but the data should be appended  to this new file.   I thought pd.files.a[[end should have taken care of that?
– user8763797
                May 7, 2020 at 20:40
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.