Python reading multiple csv files and appending them into a df

I have a folder with mutiple csv files. The name of some of them starts with the string ‘REC_’ I would like to fetch all files starting with that string and append them into a single df. How can I do that?

The way I fetch just one would be

with open(path_to_my_folder, 'r') as csvfile:
    reader = csv.reader(csvfile)

This way I need to specify the exact file in the ‘path_to_my_folder’ variable.

Answer

You talk about dataframes, so I guess you are willing to use pandas. You can iterate over your csv-files easily with the build-in pathlib-module. Eventually concatenate your frames:

from pathlib import Path
import pandas as pd

path_dir = Path(path_to_my_folder)

list_dfs = []
for path_file in path_dir.glob('REC_*.csv'):
    df_small = pd.read_csv(path_file)
    list_dfs.append(df_small)
    
df = pd.concat(list_dfs, axis=0)