Read and Write xlsx file, from pandas dataframe to specific directory

Issue

I have a function in python, that basically merges three txt files into one file, in xlsx format.
For that I use pandas package.
So I am running the python function in a certain directory. This function has the input as a specific path. Then the function takes this path, list the files of the directory, and filter the files that are needed. Meaning that, since I only want to read the txt files, I then filter the txt files. However, when I try to convert this txt files into pandas dataframe, the dataframe is None.
Also, I want to write a final xlsx to the directory where the initial files are.
Here is my function:

def concat_files(path):
    summary=''
    files_separate=[]
    arr2 = os.listdir(mypath)
    for i, items_list in enumerate(arr2):
        if len(items_list) > 50:
            files_separate.append(items_list)
    files_separate
    chunks= [files_separate[x:x+3] for x in range(0,len(files_separate),3)]
    while chunks:
        focus=chunks.pop(0)
        for items_1 in focus:
            if items_1.endswith('.Cox1.fastq.fasta.usearch_cluster_fast.fasta.reps.fasta.blastn.report.txt.all_together.txt'):
                pandas_dataframe=pd.Dataframe(example)
                pandas_dataframe.to_excel('destiny_path/' + str(header_file)+'.final.xlsx')

Solution

you need to create the folders before exporting the xlsx files.
so assuming you already have the folders created.

change this line

pandas_dataframe.to_excel('destiny_path/' + str(header_file)+'.final.xlsx')

to

pandas_dataframe.to_excel(os.path.join('destiny_path' ,str(header_file),'.final.xlsx'))

Answered By – Milad Resketi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published