Home>

python
Three Excel files are stored in each folder (9 files in total).

practice

├201114
│ ├── 1.xlsx
│ ├── 2.xlsx
│ ├── 3.xlsx
├201115
│ ├── 1.xlsx
│ ├── 2.xlsx
│ └── 3.xlsx
└201116
│ ├── 1.xlsx
│ ├── 2.xlsx
│ └── 3.xlsx

I'm trying to drop that file into a data frame and connect it with concat. In the process, I'm thinking of getting the file name with glob and repeating it with listing.
However, if I put a list in the argument of glob, I can't get the file name. What should I do? Please teach me.

import glob
import os
import pandas as pd
os.chdir ('C:/Users/Practice')
x = glob.glob ('*')
glob.glob ('*')
out: ['201114', '201115', '201116']
for i in x:
   files = glob.glob ('i/*. Xlsx') # This doesn't work
files files
out: [] # The list is empty and returned
df = pd.concat ((pd.read_excel (f)) for f in files)
  • Answer # 1

    files = glob.glob ('**/*. Xlsx')

  • Answer # 2

    files = glob.glob ('i/*. xlsx')Tofiles = glob.glob (i +'/*.xlsx')I think it is good to set it to.


    [Addition]
    This is an example of combining xlsx files under the folder as pandas.DataFrame.

    x = glob.glob ('*')
    df_list = []
    for i in x:
        files = glob.glob (i +'/*.xlsx')
        for file in files:
            df_list.append (pd.read_excel (file))
    df = pd.concat (df_list)